Aspect-Oriented Programming




What is AOP ?????

  • Aspect-Oriented Programming (AOP) complements OO programming by allowing the developer to dynamically modify the static OO model to create a system that can grow to meet new requirements. ….

Just as objects in the real world can change their states during their lifecycles, an application can adopt new characteristics as it develops.

  • Using an AOP language (such as AspectJ) or libraries (such as Spring), programmers can code this functionality once and then define where to weave it into existing Ex:

public class First{
public void method(String s){
Logger.sign_in();
// Business logic ---> AAA
Logger.sign_out();
}
}

public class Second{
public void function(Object o){
Logger.sign_in();
// Business logic ---> BBB
Logger.sign_out();
}
}

Here we can see that logging functionality is exactly duplicated in two different classes.
With AOP we do something like this (note syntax is simplified for clarity)

public aspect LogInspecter{
public Object invoke(){
Logger.sign_in();
method.execute();
Logger.sign_out();
}
}

above method.execute() will execute class "First" or "Second" method

public class First{
public void method(String s){
// Business logic ---> AAA
}
}


public class Second{
public void function(Object o){
// Business logic ---> BBB
}
}

both First and Second have no knowledge and no dependency on LogInspecter.


Comments

Popular posts from this blog

How to Connect DataBase to Java Application -JDBC

-- How to Schedule a Task --

-- Are you a Android Developer? --