At home at the end of the week, using emit to do a very simple AOP framework as a practice after learning emit these weeks. Things out, naturally to share with you, although the framework to do a relatively rough, simple, but also has been able to see a little of the embryonic form of AOP, used to play their own boring or can, of course, to use the product to certainly need to improve the long-term.
Speaking of AOP, I believe many experts in the garden have studied it, the garden also seems to have its own open source AOP project, but I haven't looked at it in a limited time, and I've found a lot of articles on AOP theory in the garden, but there seems to be no complete example of implementing a simple AOP framework (of course I'm just looking for a simple If there are omissions, please forgive. , just take the opportunity to write an example of this, so that a rookie like me can learn more about AOP.
Introduction to the concept of AOP, there are a lot of explanations on the Internet, I do not have time one by one to look at, here to give the explanation of Baidu Encyclopedia:
Aspect-oriented programming: Aspect Oriented Programming
AOP is a continuation of OOP, an abbreviation for aspect oriented programming, meaning slicing programming. A technique for dynamically adding functionality to a program without modifying the source code can be achieved through precompilation and runtime dynamic proxies. AOP is actually a continuation of the GOF design pattern, and the design pattern tirelessly pursues the decoupling between the caller and the callee, which can be said to be an implementation of this goal.
For example: Suppose that in an application system, there is a shared data must be concurrent access, first of all, the data encapsulated in the data object, called Data class, and there will be more than one access class, dedicated to the same time access to the same data object.
In order to achieve the above concurrent access to the same resources, the concept of locking lock needs to be introduced, that is, at some point, when an access class accesses the data object, the data object must be locked and locked, and then the unlocked is unlocked immediately after use, which is then accessed by other access classes.
With traditional programming habits, we create an abstract class, and all the access classes inherit this abstract parent class, as follows:
abstract class Worker{
abstract void locked();
abstract void accessDataObject();
abstract void unlocked();
}
Disadvantages:
The Accessdataobject () method requires related code such as "Lock" status.
Java only provides a single inheritance, so the specific access class can only inherit this parent class, if the specific access class also inherits other parent classes, such as another worker's parent class, will not be easy to implement.
Reuse is discounted, the specific access class because it also contains the "lock" status and other related code, can only be reused in the relevant "lock" occasions, the scope of reuse is very narrow.
A careful look at the "lock" of this application actually has the following characteristics:
The "lock" feature is not the primary or primary feature of a specific access class, and the main function of the Access class is to access data objects, such as reading data or changing actions.
The "lock" behavior is actually the main function of the specific access class can be independent and separate.
The L "lock" function is actually a longitudinal section of the system, involving many classes, many classes of methods. The following figure:
Therefore, a new program structure should focus on the vertical section of the system, such as the "lock" function of the application, the new program structure is aspect (aspect)
In this application, the "lock" aspect (aspect) should have the following responsibilities:
Provides some necessary functionality to lock or unlock the object being accessed. To ensure that lock () locking is invoked before the operation of the data object is modified, and the unlock () unlock is invoked after it has been used.