What is Aspect Oriented Programming (AOP)


A program could contains functionality(cross-cutting concerns) that affects the whole application and should be centralized in one location.such as 
  • Transaction management
  • Authentication
  • Logging
  • Security etc.
AOP creates modules (aspects in this case) and plugs them before, after or around the actual logic.Those modules are pluggable and can be added dynamically with the help of AOP.



If we consider the object oriented programming model,

Objects contains member variables and methods. We manipulate the object the object trough out the program using those variables and methods(Functions).


Lets say there is a  function which needs to executed after a certain function for each object.This is not a good design since LogMessage() is repeated several times in different Objects.



We can refine above scenarios like this, Remove LogMessage() and create a logger class and the Use a logger object in other objects.  So when ever the logging is needed we can use a object of the logger class.





logger class also can be a parent class. Then other classes can inherit the LogMessage() functionality. 

But there are several problems here,

  1. Every object that has a logger has a dependency with the logger. So the parent of those object would be the logger, not a business object. That means the classes has a dependency with another class which is not in the business domain. logger class has no business value.It is just adding support to the system.
  2. Even though we were able to separate the logging functionality by creating the logger class, we still some code changes in order have the LogMessage() functionality.And has to put a code every where the logging functionality is needed.
  3. What if we want to change the Log message? Then we would have go each and every class, then change the logging message.Cannot all be changed at once.(We might be able to use interfaces and a intelligent design in-order overcome this)



As a solution for the above problems, we can use Aspect Oriented Programming (AOP)

Except for creating a class for logging, we create Aspects. It is like a class with special characteristics. what we are doing here is using a aspect instead of object.We replaced logging object with logging aspect.
Here we use Aspect Configuration instead of a reference. We don't need a reference to  trigger the logging functionality.Aspect configurations tells which aspects applies to which methods in which classes.


Aspects can be configure to run before or after a Target Method.

Steps:
  1. Write the Aspects.
  2. Configure the Aspects.


AOP Concepts and Terminology

  • Join point
  • Advice
  • Pointcut
  • Introduction
  • Target Object
  • Aspect
  • Interceptor
  • AOP Proxy
  • Weaving

We were able to cover the concepts of AOP and I'm hoping to show how AOP is used in Spring in my next post.Those concepts will be covered in that post with code examples in Spring.


SHARE

Harsha Jayamanna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment