Introduction to AOP (Aspect-Oriented Programming)

Source: Internet
Author: User
Introduction to AOP (Aspect-Oriented Programming)

Reference: http://wayfarer.cnblogs.com/articles/241012.html

Http://www.microsoft.com/china/msdn/library/langtool/vbnet/AOPArticle.mspx? MFR = true

Aspect-Oriented Programming(AOP)Is the Palo Alto Research Center of Xerox.(Xerox PARC)Last century90A programming paradigm invented by the times, which allows developers to better separate tasks that shouldn't be entangled with each other (such as mathematical operations and Exception Handling.AOPMethods have many advantages. First, because the operation is more concise, the performance is improved. Second, it makesProgramMembers can spend less time rewriting the sameCode. In short,AOPIt can provide better encapsulation for different processes and improve future interoperability.

1. Object-oriented Thinking(OOP)

In the object-oriented software idea, everything is an object. The construction of a software system is like building blocks.---The software system is visually represented by a group of objects and their relationships. In an object-oriented system, basic features such as encapsulation, inheritance, and polymorphism are everywhere.The Application of Object-oriented thinking can shorten the development cycle when designing systems with larger scales and more complex logic. However, the only problem with object-oriented design is that it is static and closed in nature, and any minor changes to requirements may modify the structural relationship of the original object system. How can we not have a good structure, reusability will be poor.

The possible solution to this problem is the design mode.GofRecording the design experience of object-oriented software as a design pattern allows people to reuse the successful design and architecture more easily and easily, helping developers make choices that are conducive to system reuse. The design pattern solves specific design problems, makes object-oriented design more flexible and elegant, and achieves better reusability. However, although the Design Pattern provides us with a design model and criterion, we can use the object-oriented features to the greatest extent, such as inheritance and polymorphism, separation of responsibility, and inversion of dependency, abstract-oriented and interface-oriented, finally designs flexible, scalable, and reusable class libraries and components, and the entire system architecture. In the design process, various modes are used to demonstrate the behavior, exposed interfaces, relationships between objects, and forms of objects at different layers.

However, in view of the particularity of object encapsulation,"Design Mode"Is always in the interface and abstract.ArticleBut cannot do anything inside the object.

Aspect-Oriented Programming(Aspect-Oriented Programming,AOP) Can solve this problem. It allows developers to dynamically modify staticOoModel to construct a system that can continuously grow to meet new requirements, just as objects in the real world will constantly change themselves in their lifecycles, applications can also have new features in development.AOPUse"Cross-cutting"And encapsulate the behavior that affects multiple classes into a reusable module."Aspect", That is, aspect. The so-called"Aspect"Simply put, it encapsulates the logic or responsibilities that are not related to the business but are called by the business module, such as transaction processing, log management, and permission control, this helps reduce system code duplication, reduce coupling between modules, and facilitate future operability and maintainability.

Aspect-oriented thinking(AOP)

AOP ( Aspect-Oriented Programming , (Aspect-Oriented Programming). It can be called OOP ( object-oriented programing , (Object-Oriented Programming. OOP introduces concepts such as encapsulation, inheritance, and Polymorphism to build an object hierarchy to simulate a set of public behaviors. When we need to introduce public behavior to scattered objects, OOP seems powerless. That is to say, OOP allows you to define the relationship from top to bottom, but it is not suitable for defining the relationship from left to right. For example, log function. Log Code is often horizontally distributed across all object layers, but it has nothing to do with the core functions of the objects it spreads. This is also true for other types of code, such as security, exception handling, and transparent persistence. This unrelated code is called the cross-cutting ( cross-cutting ) code, in the OOP design, it leads to a large number of code duplication, which is not conducive to the reuse of each module.

the AOP technology is the opposite, it uses a technology called " Cross-section " , anatomy of the encapsulated object and encapsulate the public behaviors that affect multiple classes into a reusable module, and name it "aspect" . The so-called " " , simply put, is to put those that have nothing to do with the business, but it is encapsulated by the logic or responsibility that the business module calls together to reduce the repeated code of the system, reduce the coupling between modules, and facilitate future operability and maintainability. AOP represents a horizontal relationship, if the " Object " is a hollow cylinder, the encapsulation is the properties and behavior of the object. The Aspect-Oriented Programming Method is like a sharp edge, which opens these hollow cylinders to obtain internal messages. The cut section is the so-called aspect . Then it restored these cut sections with the skill of winning the power of heaven without leaving any trace.

Use"Cross-cutting"Technology,AOPThe software system is divided into two parts: core focus and cross-cutting focus. The main process of business processing is the core focus, and the part that has little to do with it is the cross-cutting focus. One feature of cross-cutting concerns is that they often occur in multiple places of core concerns, and are similar everywhere. For example, permission authentication, logs, and transaction processing.AOPThe role of this function is to separate the various concerns in the system from the core concerns and the cross-cutting concerns. AsAvanadeSenior Solution Architect of the companyAdam MageeThat is,AOPThe core idea is"Separates the business logic in an application from the common services that it supports."

ImplementationAOPThe technology is mainly divided into two categories: first, the dynamic proxy technology is used to describe the message by intercepting the message, so as to replace the execution of the original object behavior; second, use static weaving to introduce specific syntax Creation"Aspect"So that the compiler can weave the relevant"Aspect". However, the same way is achievedAOPThe technical features are the same:

1,Join point(Joint point): a clearly defined identifiable point in the code.
2,Point cut(Cut point): a method for specifying a joint point by configuring or encoding.
3,Advice(Notice): indicates a method for performing a cross-cutting operation.
4,Mixin(Mixed in): introduces a new behavior by adding an instance of the class to the instance of the target class.

To better understand these terms, you can regard the joint point as a point defined in the Program Stream. A good example of a joint point is: when the Code calls a method, the point that is called is considered as a joint point. The cut point is used to specify or define the joint points to be intercepted in the Program Stream. A cut point also contains a notification that occurs when the joint point is reached. Therefore, if you define a cut point on a specific method called, when you call this method or joint point,AOPThe framework intercepts this cut point and executes a cut point notification. There are several types of notifications, but the most common case is to regard them as another method to be called. When calling a method with a cut point, the notification to be executed will be another method to be called. The notification or method to be called can be an intercepted method in the object, or a method in another mixed object. We will further explain the problem later.

3.JavaUnder the PlatformAOPFramework

1. aspectj

AspectjDefine a setJavaThe Language extension system allows developers to easily perform aspect-oriented development.AspectjIn2002TransferredEclipse FoundationTo become an open-sourceCommunityMediumAOPThe technology pioneer is also the most popularAOPTool.

2. aspectwerkz

AspectwerkzIs based onJavaDynamic and lightweightAOPFramework.2005Year1Month,AspectjAndAspectwerkzReach an agreement, agree to combine the results of the two, take its essence to create a single tool. The first release of their cooperation wasAspectj 5, It extendsAspectjLanguage to supportAnnotationDevelopment style and support for similarAspectjCode style.Aspectj 5OrJava 5Language Features provide completeAOPYes. Currently,AOPIs fully integratedSpringContext of transaction management, logs, and other features.

3. JBoss AOP

4. Spring AOP

As a lightweightFramework,SpringIn the development of lightweightJ2EEIs widely used. It usesIOCMode (Inversion of ControlTo control the reverse mode.AOP, Usually calledSpring AOP.

Thu. NetUnder the PlatformAOPFramework

1. aspect #

ACLIOfAOPThe integrated compatibility framework provides built-in languages for declaration and configuration, and uses reflection, proxy, and other mechanisms.Aspect #Is based onCastleDynamic proxy technology.CastleOriginated fromApache aveonProject to implement a lightweightIOCContainer.Aspect #Yu2005Year6The month is includedCastle.

2. sping. net

PopularJava spingOne of the frameworks. NetVersion. In the next versionAOP.

3. Rail (runtime Assembly instrumentation Library)

RailThe framework is used to compile class code in a timely manner on virtual machines. Refer:Http://rail.dei.uc.pt/index.htm

4.Aspectdng

Its implementation technology is based onRail.RailBelongIlIt is a custom setXMLFormatIlmlLanguage, can split the original assemblyIlmlTo modify and expand the static assembly, so as to achieve static weaving. BecauseAspectdngYesIlThe code under the level is woven, so. NetPlatform, not subject to specificProgramming Language.

5. Eos

used for C # is an aspect-oriented extension. Like eos aop and aspectdng , however, in terms of syntax definition, it is more similar to the implementation of aspectj On AOP . It extends the C # syntax, aspect , introduce , and before , after , and provides a dedicated EOS compiler. Reference: http://www.cs.virginia.edu /~ EOS/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.