Friday, May 28, 2010

Introduction to AOP (Aspect Oriented Programming)





Introduction to AOP (Aspect Oriented Programming)

AOP is a programming technique which isolates the secondary problems from the main problem. For example, solving the Salary Processing is the main problem.  Logging, Security, Transaction, etc are not the main problems but the issues need to be addressed with the main problem.

AOP is not a replacement for OOP, but a complement. Addressing the main problem with AOP is not a wise idea when we have proven, easy to develop OOP. Solving secondary problems along with main problem in OOP will burden the design with extra load. So the best approach is to solve the main problem with OOP and secondary problems with AOP.

How AOP works

In AOP approach, we solve all the secondary problems independently, and finally we will weave the secondary solutions into the mail solution.  Weaving is the process of joining main and secondary logics together. The object weaved is called Target

Types of AOP

There are two types in AOP
  1. Static
  2. Dynamic

Static AOP merges the main and secondary logic during the compilation time itself, so the running application has no overhead. In Java Static AOP is achieved by merging both the logics in bytecode.

Dynamic AOP on the other hand merges the main and secondary logic during the runtime, this gives more flexibility, and during runtime we can include different secondary logics according to the business logic. But remember flexibility always comes with its own price. The main technique used in java to achieve dynamic AOP is proxies.
  
AOP building blocks

Like every technology or concept , AOP also comes with lot of jargons and buzzwords. There are the actual building blocks of the AOP.

JoinPoint is the point in the application where we can insert secondary logic, for example method execution, object instantiation, etc.

Advice  is the actual secondary logic. Advice can be classified into different types according to the point where they are getting executed. Advice can be executed just before the joinpoint or after the joinpoint, etc.

Pointcut is collection of joinpoints . it can be a like all the methods in a class or all the classes in a packages or all dao classes in some packages, etc.

Aspects are the bundle of pointcuts and advice.  Aspect joins the advices with pointcuts.


Types of Advices

Based on the point of execution Advices can be classified as follows

1. Before Advices -  executes before the joinpoint
2. After Advice -  executes after the joinpoint
3. Around Advice - executes before and after the joinpoint
4. Throws Advices - executes when exception thrown from joinpoint
5. After Return Advise - executes when the joinpoint returns value.

For AOP example please refer – Two simple approaches to Spring AOP

Classification : simple introduction to AOP, AOP Concept, Simple AOP, what is advice, what is pointcut, what is joinpoint, what is aspect, what is weaving, what is target

No comments:

Post a Comment