Tuesday, February 19, 2008

Aspect Oriented Concepts and terminologies

As OOP has its own concepts, AOP also defines several concepts and terminologies to model the methodology. It is essential to have a clear idea about these concepts because obviously you are going to apply them in your code base. The standard AOP concepts are Aspect, Advice, Join point, Point cut and weaving. Each of these will be discussed below.
Aspects are the actual concerns or the problems that we are going to apply AOP. These are basically cross cutting concerns like error handling, transaction management. Aspects are little tricky because we cannot directly encapsulate the logic behind them in classes or objects. But much more like classes in OOP. Aspects are reusable and the entire implementation on one concern can be included in one aspect. In logging example instead if calling LogMethodCall (); in each and every method we can call all the methods through the logging aspect. This approach decreases the maintenance cost in huge amount. Another example of Aspect is screen rendering or screen update aspect. For instance think about a particular system which need screen update to be done after each and every function or method calls. This can be done by calling the relevant object just before exiting from method or function and definitely would be a big headache for the programmer. Therefore, the scenario can be modularized as an aspect in AOP world. Aspects can be identified as two types namely Static Aspects and Dynamic Aspects. Here the meaning of Static, Dynamic little deviates from natural sense of them. Dynamic does not mean runtime but defines where to insert additional behavior in to code. Static defines inserting additional methods, expressions in to existing aspect and does not concern about where to apply them.
More technical definition for Aspect can be provided as ‘aspect is a combination of point cut and advice’.
Advice is the actual code that contains the logic of Aspect. Advices can implement using existing programming languages and the languages designed to provide more support for AOP like AspectJ. In screen update scenario, advice contains how to render the pixels, is it whole screen or only the changed pixel frames, and the color schema used. Simply the action should take place in a particular point. Advice code can be grouped into two categories. One is ‘before’ and other is ‘after’ advice code. Before part executes before reach to a particular point called join point in a code base and after part executes just after reach to that join point. Little technical stuff here.
If I go to implementation level that is unavoidable when talk about advices, so the pseudo code looks like,
before(): goto_university() {
// The piece of code should execute before goto_university
System.out.println("Just before goto_university ");
after():goto_university () {
// The piece of code should execute after goto_university
System.out.println("Just after goto_university ");

After advice can be broken into two parts called, after returning() and after throwing(). After returning part contains piece of code which should be executed if the method returns from it normally. After throwing part executes when exception throws form the method or any error returning happens.

Pseudo code looks like – after() returning : goto_university() { // ……….}
– after() throwing : goto_university() { // ……….}

Join point and point cut
Join points are the points of program code where the advices should be applied. The point should be well defined and can be pointed in method calling, beginning of a loop, condition check, variable assignment and when initializing object or a class. This is the point where the additional program code should get executed.
Point cut can be defined as collection of join points or set of join points identified by their IDs. Single advice may be required to execute in several locations in base program. Point cuts facilitate for that issue by holding group of join points in similar category.

Signature of setting a point cut.
pointcut setter(): target(University_Class) &&
(call(void insertStudent(id , name)) ||
call(void deleteStudent(id , name)) ||
call(void updateStudent(id , name , address)));

In above example the point cut groups three method calls (join points) insertStudent(), deleteStudent(), updateStudent() in University_Class and this point cut can be called in an advice. So every call before, after above methods advice get executed. This is the approach of AOP in real world.
In practical world we write the program source code separately and aspect code separately. How can we merge those two separate implementations? To find answers for these questions, we will have to understand the AOP architecture. The merging process of application source code with its aspect code is called “Weaving” or “Aspect weaving”. Out put is called woven code in AOP terminology. The woven code is provided for complier it generates the executable program, which can be run on virtual machine or without any framework.

There are two types of weaving techniques called ‘static weaving’ and ‘dynamic weaving’. Most common approach is static weaving. In static weaving, source and the aspect goes through the weaver and out put cannot be modified or cannot be removed. AspectJ is a common example for static weaving. After weaving it is very difficult or impossible to separate aspects and application program. So there are limitations.
(Static weaving)
(Dynamic weaving)

In dynamic weaving aspects can be applied in runtime. So it is capable of plug, unplug and update aspects as well as point cuts in an application code while it is executing. This method provides great advantage in maintenance projects and when we make security patches for running applications. But there are some drawbacks such as performance issues because weaving is done in runtime or load time.