Aspect-Oriented Programming (AOP)

Leave a comment

March 30, 2010 by huionn

In Wiki, AOP is defined as

In computing, aspect-oriented programming (AOP) is a programming paradigm in which secondary or supporting functions are isolated from the main program’s business logic. It aims to increase modularity by allowing the separation of cross-cutting concerns, forming a basis for aspect-oriented software development.

Aspect-oriented programming entails breaking down a program into distinct parts (so-called concerns, cohesive areas of functionality). All programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g. procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they "cut across" multiple abstractions in a program.

In my opinion, AOP is part of evolution in programming paradigm.

functional programming is VERB which performs action

object-oriented programming is NOUN which encapsulates object’s states and behaviors

aspect-oriented programming is ADJECTIVE which describes object

 

For example (sample codes from AspectJ in Action):

public class MessageCommunicator {
    private Authenticator authenticator = new Authenticator();

    public void deliver(String message) {
        authenticator.authenticate();
        System.out.println(message);
    }

    public void deliver(String person, String message) {
        authenticator.authenticate();
        System.out.println(person + ", " + message);
    }
}

public aspect ProfilingAspect {
    pointcut publicOperation() : execution(public * *.*(..));

    Object around() : publicOperation() {
        long start = System.nanoTime();
        Object ret = proceed();
        long end = System.nanoTime();
        System.out.println(thisJoinPointStaticPart.getSignature() + " took "
                + (end – start) + " nanoseconds");
        return ret;
    }
}

 

In technical term, ProfilingAspect is weaved into MessageCommunicator.deliver(..) so that whenever MessageCommunicator.deliver(..)  is invoked, its execution time will be recorded. In other word, by applying ProfilingAspect,, MessageCommunicator has become ProfiledMessageCommunicator (“Profiled” is an adjective).

This shows how powerful is AOP and its potential in simplifying software complexity by separation of concern.

In conventional way, to achieve this, every methods in class need to be modified or alternatively, the class extends a superclass/implements a interface and then use Decorator design pattern to add additional behavior. Although it is not difficult, it adds unnecessary complexity into the codes and may introduce bug into the codes.

By using AOP, additional behavior will be introduced into every methods that match signature pattern public * *.*(..) without modifying the classes. Does it look like magic? I think so…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: