Aspect Oriented Programming Miscellaneous

Source: Internet
Author: User

So far, Aspect Oriented Programming has been well known by developers and its short name is AOP ). It was developed from the Xerox PARC lab programming paradigm in 1990s. Known as the continuation of oop, oop is mainly aimed at the abstract encapsulation of domain problems in the business processing process to form domain objects and better describe natural domain problems. Aop mainly processes the separation of processing logic steps in the business processing process to reduce the coupling of business logic. Our developers only need to focus on the core and actual logic of the field during the development process. SOC (separation of concerns) is proposed in structured programming SP. It has always been called one of the most effective methods for complex software and has been applied to different software practices, for example, the presentation layer pattern MVC, MVP, PM, SOA for service-oriented programming, and abstract programming. Similarly, AOP is an application of SOC, which separates the process of business logic steps in the domain, yes, we can focus only on our core business in the real field. I personally think that AOP is a compensation for OOP, not an innovation.

The implementation of AOP has been mentioned in the casual IOC/AOP essay Directory, which is mainly divided into static implantation and dynamic injection. The main difference is that different code periods, compilation periods, and runtime. In fact, there are some common implementation methods in 3rd that bring AOP, such as ActionFilter and Handler in asp.net mvc, which are usually fixed in the template method for processing, releasing the main logic to be implemented.

AOP consists of two important parts: 1. matching rules. In the implementation of common AOP frameworks, methods, classes, assembly, and package names or fixed features are used for exact matching or fuzzy matching, the Annocaton ,.. net Attribute. 2hack logic. For AOP, It is a hack targeting the method boundary. For a method, there are three visible boundary, namely, entering, ending, and exception. AOP inserts the hack logic in the three boundaries to achieve the separation of common public services.

Application scenarios of AOP: common process sequence in cache, log, exception handling, complex permissions, transactions, and performance count is the main scenario where logic is used.

The following describes the implementation of AOP in a well-known centralized language:

1: Python

Implementing AOP in Python will be the simplest, thinking that decorating a function in Python is a natural AOP,

Def aopLogic (f ):
Def warp ():
Print ("befor ....")
F ();
Print ("after ...")
Return warp

@ AopLogic
Def myMethod ():
Print ("my method pass! ")

Def myMethod1 ():
Print ("my method pass! ")

MyMethod ()
Print ("---------------------")
AopLogic (myMethod1 )()
Print ("---------------------")
Input ("finish ..")

Output:

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" SNAGHTMLf06e3 "border =" 0 "alt =" SNAGHTMLf06e3 "src =" http://www.bkjia.com/uploads/allimg/131228/1205514B2-0.png "width =" 626 "height =" 222 "/>

The python decoration function starts with @, followed by any function. The above is a non-parameter aop demo. We can see that myMethod = aopLogic (myMethod (no decoration) is decorated )). If the parameter-specific decoration function is used, aopLogicargs...) (myMethod (no decoration )).

2: javascript

For dynamic explanatory scripting languages such as javascript, there is a dynamic feature that must replace the policy for defining methods.

 

Function aopTest (obj, func ){
Var old = obj [func];
Obj [func] = function (){
Console. log ("before ....");
Old ();
Conso le. log ("after ....");
}
}

Var obj = {
"Test": function (){
Console. log ("body ......");
}
};

AopTest (obj, "test ");

Obj. test ();?

Output:

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" SNAGHTML2c4559 "border =" 0 "alt =" SNAGHTML2c4559 "src =" http://www.bkjia.com/uploads/allimg/131228/1205511491-1.png "width =" 631 "height =" 239 "/>

The above code and actual aop feel that there is always something missing to write. We have to use aopTest (obj, "test") to automatically match the missing framework or native AOP "); this type of method is used for manual matching. You may say that it is a simple replace of a Dynamic Language, and AOP is somewhat far-fetched. In the current situation, I also think that there is a lack of automatic principles. However, if we do these things based on some js frameworks, everything will take its place. For example, the ioc Injection mechanism provided by angularjs talks about angularjs DI (Dependency Injection) and uses it to create automatic aop matching packaging for services.

3: java and c # Strong Language

AOP for this type of language has mentioned many random directories of IOC/AOP. There are two types of static implantation and dynamic injection.

Static embedding targets Language Mechanisms with intermediate languages, java bytecode, and c # MSIL. They introduce specific syntax to create "Aspects ", and insert some specific logic to the matching method during the static compilation period, such as java aspectJ ,.. net PostSharp. Use PostSharp to parse the open-source framework based on mono. cicel IL and the MSBuild compilation mechanism to implant specific logic. For details, see the PostSharp article in the IOC/AOP document directory.

Dynamic Injection: Design Mode: Dynamic proxy mode: override the original class of the parent class.) The hack logic of the method uses the message capture method to describe the message to replace the execution of the original object behavior. Common AOP frameworks on the market rely on the IOC framework to handle references. For example, spring, spring.net, Castle, and el piab.

This type of code is the most common, so it is not demonstrated here to post it.

 

The advantage of AOP lies in the cross-cutting separation of business logic, which decouples business logic and becomes discrete and loose. This allows different business people to focus on their own real domain issue and domain logic, the reuse of public logic is large, and the code is neat and tidy to reduce the repetition of all evil.

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.