What is AOP?

Source: Internet
Author: User

What is AOP?

AOP is a continuation of OOP and short for aspect oriented programming, which means Aspect-Oriented Programming. AOP is actually a continuation of the gof design model. The design model tirelessly pursues decoupling between callers and callers. AOP can be said to be an implementation of this goal.

For example, assume that in an application system, a shared data must be accessed concurrently. First, encapsulate the data in a data object called data class, there will be multiple Metadata classes dedicated for accessing the same data object at the same time.

To achieve the preceding concurrent access to the same resource, the lock concept must be introduced. That is to say, when a metadata class accesses this data object at a certain time point, the data object must be locked, unlock unlocked immediately after use, and then provide access to other unlock classes.

Using traditional programming habits, we will create an abstract class. All the class classes inherit this abstract parent class, as shown below:

Abstract class worker {

Abstract void locked ();
Abstract void accessdataobject ();
Abstract void unlocked ();

}

Disadvantages:

    • The accessdataobject () method requires a "Lock" status or something like that.Code.
    • Java only provides single inheritance. Therefore, a specific worker class can only inherit this parent class. If a worker class also inherits other parent classes, for example, another worker parent class, it cannot be conveniently implemented.
    • Reuse is compromised. Because the specific classification class also contains Code related to the "Lock" status, it can only be reused when there is a "Lock". The Reuse scope is very narrow.

      The "Lock" of this application has the following features:

    • The "Lock" function is not the primary or primary function of a specific category. The primary function of the category is to access data objects, such as reading or changing data.
    • The "Lock" behavior can be separated from the main functions of a specific category.
    • The "Lock" function is actually a vertical aspect of the system and involves many classes and methods. For example:

Therefore, a newProgramThe structure should focus on the vertical aspect of the system. For example, the "Lock" function of the application. The new program structure is aspect (aspect)

In this application, aspect has the following responsibilities:

Provides essential functions to lock or unlock the accessed objects. To ensure that all the operations on the data object can be called to lock (), after it is used, call unlock () to unlock.

Application Scope of AOP

Obviously, AOP is very suitable for developing J2EE container servers. Currently, JBoss 4.0 is developed using the AOP framework.
The specific functions are as follows:
Authentication permission
Caching Cache
Context passing content transmission
Error Handling
Lazy Loading
Debugging debugging
Logging, tracing, profiling and monitoring record tracking optimization Calibration
Performance Optimization
Persistence persistence
Resource pooling resource pool
Synchronization
Transactions transaction

Is AOP necessary?

Of course, the above application examples have also been solved without using AOP. For example, JBoss 3. xxx also provides the above application functions, but does not use AOP.

However, using AOP allows us to understand software systems from a higher abstract concept. AOP may provide a valuable tool. This can be said: Because the AOP structure is used, the source code of JBoss 4.0 is much easier to understand than JBoss 3.x, which is very important for a large complex system.

From another aspect, it seems that not all people need to care about AOP. It may be an architectural design option. If you choose a J2EE system, the above general aspects of AOP have been implemented by J2EE containers. J2EE application system developers may need to pay more attention to the aspect of industrial applications.

Specific implementation of AOP

AOP is a concept that does not set the implementation of a specific language. It can overcome the disadvantages of a single inheritance feature language (such as Java). Currently, the specific implementation of AOP has the following items:

Aspectj (TM): created in Xerox PARC. has a history of nearly ten years and is mature.
Disadvantages: Too complicated; destroys encapsulation; Special Java compiler is required.

dynamic AOP: Uses JDK's dynamic proxy API or bytecode processing technology.

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.