Thursday, January 6, 2011

Aspect Oriented Programming - What is it , Why is it for me and How to do it



Enough of the Jargons ... Procedure Oriented, Object Oriented....and now
 what is Aspect Oriented Programming ?




What is Aspect Oriented Programming ?
Just imagine you have system for which you need add a set of  log statements
at the run-time. and say you should not modify the sources. How do you do it ?

OR put it in other way , say your application which is in execution needs
to be audited -say how many lines code is modified by each test case ?
This can be done by an adding a  counter in you testcase for each line
it accesses. Easy to do it at source if there are say, 10 test cases.
But , just imagine doing it for some 10000 test cases. We can simply forget
doing anything at the source level in these scenarios since this book- keeping
activity will consume lot of man-days and we would be more than happy if
we can hook something to our tests at the time of execution rather
than doing this at source level.This is exactly what AOP does for us.

AOP is very useful in cases of  are like  auditing, logging, security,
exception handling without having to modify the original business logic sources.
( Let us understand  each of these issues as "concerns" that are cross-cutting
 the breadth of the application )

---------click below on "more" for a detailed complete application --------

Why AOP


In OOP real world problems are mapped to Objects. OOP encourages software
re-use using encapsulation, inheritance and polymorphism. It is very difficult
 to cleanly maintain separate "concerns"  into modules. An attempt to do a minor
change in the program design may require several updates to a large number of
unrelated modules. 

With AOP, we start by implementing our project using our OO language
 (for example, Java), and then we deal separately with crosscutting concerns
in our code by implementing aspects. AOP implementation coexists
with the OOP by choosing OOP as the base language.
AOP addresses each aspect separately in a modular fashion with
minimal coupling and duplication of code

Aspect Weaver : The aspect weaver integrates aspects into the locations specified
by the software as a pre-compilation step.


Aspect Waver
Above diagram shows , Aspect waver combines .java ( Java Classes )
with .aj (Aspect Class) to create final version of byte code after merging aspect to
main code without modifying Java source files.
Aspect Oriented Programming is a concept so it is not bounded to particular
 programming language.  But reach community got great interest in java language.
Following is the list
of few AOP + Java implementation
  1. AspectJ
  2. AspectWerkz
  3. Hyper/J
In this post i am going to explain AspectJ implementation.
AOP pillars : Following terminologies are used in AOP
Joint Point : Joint point represent well defined points in program’s execution.
Typical join point in AspectJ include
  • method/constructor calls
  • method/constructor execution
  • field get and set
  • exception handler execution
  • static & dynamic initialization
Point Cut : Point cut is a language construct that picks out a set of join points
based on defined criteria. The criteria can be explicit function name or wild cards.
Advice : Point cuts are used in he definition of Advice. An advice is AspectJ is
 used to define additional code to be executed before , after or around joint conditions.
Following example explains all these concepts. Here is a step by step approach to
try first hand on aspect oriented programming.
1. Get AspectJ latest version and install it.
2.  Write a simple Java class
1: public class TestClass 
2: {
3:  public static void display(){
4:   System.out.println("In Display");
5:  }
6: 
7:  public static void main(String[] args){
8:   display();
9:  }
10: }
11: 

Here we have declared normal java class with simple public void display method
 as well as main method.

3. Write a simple aspect class
1: public aspect TestClassAspect 
2: {
3:  //defining pointcut, exection of display() method
4:  public pointcut displayCall(): call (public static void display() );
5: 
6:  before() : displayCall(){
7:   System.out.println("Before DisplayCall");
8:   System.out.println( thisJoinPointStaticPart.getSignature().getName() + " End...");
9:  }
10: 
11:  after() : displayCall(){
12:   System.out.println("After DisplayCall");
13:   System.out.println( thisJoinPointStaticPart.getSignature().getName() + " End...");
14:  }
15: 
16: }
17: 

We can define normal methods and variables as well in this aspect class.
Here we have defined join points, point cuts and advice. We have defined point cut named  “displayCall” which collects all joint point for method call with signature “public static void display()”.Once we have this point cut declared , we can use this to defining any advice after,before or around this. In next step, two advices have been defined ,
one is before point cut and another is after point cut.
Here point cut is noting but display method call, so this advice will get execute before and after the display method call.



AspectJ Compiler

Initially, you have to download the most recent version of AspectJ, available for free at its official site and install it. Compiling and running our example is very easy. Just write:
ajc TestClassAspect.aj TestClass.java
java TestClass
You should note that the source code at TestClass.java didn't change. To return to the original functionality of your program, just use your Java compiler to re-compile it.
The list of tools that support AOP in java
In summary, AOP is a very powerful programming construct which is not yet fully exploited and has the potential to cross certain boundaries which traditional constructs cannot. AOP is a facilitator for OOP and not a case of "either this - or that" .

Hope you like it. Try out more on this . I will try to put more examples on this in future,


No comments:

Post a Comment

subversion video