Suppose an application system, there must be a shared data concurrently accessed simultaneously, first, the
data is encapsulated
in
a data object
, the called Data Class, at the same time, there will be access to a plurality of classes, designed to access at the same time this same data object.
In order to accomplish the above concurrent access to the same resources, you need to introduce the concept of lock Lock, that is, at some point, when there is an access class to access the data object, the data object must be locked Locked, unlocked immediately after running out unLocked, then for other access type of access.
Using traditional programming practice, we will create an
abstract class
, all access classes inherit the abstract parent class, as follows:
1
2
3
4
5
|
abstract
class
Worker {
abstract
void
locked();
abstract
void
accessDataObject();
abstract
void
unlocked();
}
|
accessDataObject () method requires the relevant code "locked" state and the like.
Java provides only single inheritance, so the specific access class can only inherit the parent class, even if the specific access classes inherit other parent, such as another parent Worker will not be easy to achieve.
Reuse be discounted, because the specific access class is also contained in the code "lock" state or the like, can be reused in the relevant case the "lock", reuse very narrow range.
Careful study "lock" the application, it actually has the following characteristics:
The main function of the primary or "lock" feature is not specific access type, access class main function is to access the data object, such as reading data or changing action.
"Lock" function is actually a longitudinal section of the system, involving a number of classes, methods, many classes. Such as the right:
Therefore, a new program structure should be concerned about the longitudinal section of the system, such as the application of the "lock" feature, the new program structure is the aspect (aspect)
In this application, the "lock" aspect (aspect) should have the following responsibilities:
Provide some of the necessary features to achieve locking or unlocking function of the object being accessed. To ensure that all modifications in the
data object
before the operation can call the lock () lock, use it after completion, call unlock () Unlock.
Obviously, AOP ideal for developing J2EE container server, JBoss 4.0 is the use of AOP framework for development.
Specific functions are as follows:
Authentication Permissions
Caching
Cache
Context passing
content delivery
Error handling
Error handling
Lazy loading delay loading
Debugging Debugging
logging, tracing, profiling and monitoring recording tracks Picture
Performance optimization performance optimization
Persistence Persistence
Resource pooling
resources pool
Synchronization Synchronization
Transactions
Transaction
[AOP necessary? ]
Of course, the above-mentioned application example in the case of not using AOP, have been resolved, e.g. JBoss 3.XXX also provides the above applications, and does not use AOP.
However, using AOP allows us from a higher abstraction to understand the software system, AOP may provide a valuable tool. It can be said: because the structure using AOP, JBoss 4.0 source code for a large and complex system which is very important than JBoss 3.X much easier to understand.
From another aspect that seems not everyone needs to be concerned about AOP, it may select an architectural design, if you choose J2EE system, the general-purpose aspects of AOP concerns have been achieved J2EE container, J2EE application developers You may need to pay more attention to industry application aspect.
Traditional programs often can not show some natural fit for a single
program module
or the behavior of several closely related program modules, AOP will be called crosscutting this behavior, they crossed the boundaries of the typical duties of a given programming model. Implement crosscutting behavior is scattered,
software designers
will find it difficult to think this behavior with normal logic, implementation and change. Some of the most common of these crosscutting behavior such as the following:
Logging, tracking, optimization and monitoring
Processing services
Endurance of
Optimize performance
Pool resources, such as
database connection pool
management
Unified certification system, rights management, etc.
Abnormal capture and processing applications system
Crosscutting behavior for specific industrial applications
In front of several crosscutting behavior has been closely watched, there have been a variety of valuable applications, but perhaps the next few years, AOP contribution to industry-specific applications will become the focus of concern.
Implement the project
AOP is a concept that does not achieve the setting of a specific language, it can overcome those disadvantages only single inheritance features of the language (such as Java), AOP concrete realization of the following items:
AspectJ (TM): Xerox PARC was founded nearly a decade of history, mature.
Cons: too complex; break encapsulation; require specialized Java
compiler
.
Dynamic AOP: JDK dynamic proxy API or
bytecode
Bytecode processing techniques.
Based on specific projects dynamic proxy API are:
JBoss 4.0 JBoss 4.0 Server
Project-based bytecode are:
aspectwerkz ,spring
effect
Process-oriented
programming has been somewhat distant from us,
object-oriented programming
being dominated software world. As each new
software designers
are required to master how to transform functional requirements into one class, and define their
data members
when the behavior, and the complex relationship between them, aspect-oriented programming (Aspect-Oriented Programming, AOP) brought us new ideas, new ideas, new models.
If the object-oriented programming is concerned about the demand functions into different and independent, well-packaged class and have them with their own behavior, relying on inheritance and
polymorphism
and so on to define their relationship, then; so Oriented Programming is It is to be able to separate the general class of functional requirements from irrelevant which can make a lot of class to share behavior, once the changes do not have to modify a lot of class, but only need to modify this behavior can be.
Aspect Oriented Programming
is a new mode of exciting. For on the development of software systems, its influence will be applied and has decades of history of
object-oriented programming
as big.
For
section programming and object-oriented programming is not only competing technologies but also a good complement each other. Object-oriented programming is mainly used for the same target level of public
behavior modeling
. Its weakness is a common behavior among multiple unrelated objects used in the model. And this is precisely where Oriented Programming suitable. With AOP, we can define the relationship between cross and applies these relationships across modules, different from each other object model. AOP also can let us hierarchical functional rather than embedded functionality, making the code more readable and easier to maintain. Object-oriented programming and it worked well