Aspect Oriented Programming (AOP)


Aspect Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects [1].

What is Aspect Oriented Programming (AOP)?

Aspect Oriented Programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. Cross-cutting concerns are the aspects of the program that can affect other concerns. These concerns often can’t be cleanly decomposed from the rest of the system in both the design and implementation, and can result in either scattering (code duplication), tangling (significant dependencies between systems), or both. The separation of cross-cutting concerns happens by adding additional behavior to existing code without modifying the code itself, instead separately specifying which code is modified via a “pointcut” specificationas shown in figure \ref{fig__colon__figure1}. AOP forms a basis for Aspect Oriented software development. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. Aspect Oriented programming entails breaking down program logic into distinct parts (so-called concerns, cohesive areas of functionality). Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., functions, procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. Examples of crosscutting concerns include process synchronization, location control, execution timing constraints, persistence, and failure recovery. There is also a wide range of algorithms and design patterns that are more naturally expressible using AOP.

\label{fig:figure1} Modularizing cross-cutting concerns using Aspect Oriented Programming (AOP)

AOP has some concepts and terminology. Here are some of them: 
  • Aspect: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a cross-cutting concern.
  • Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception.
  • Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice.
  • Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP.
  • Introduction: declaring additional methods or fields on behalf of a type.
  • Target object: object being advised by one or more aspects. Also referred to as the advised object.
  • AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on).
  • Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime.
Here is an example for a simple AOP program that whenever the execution of a function starts, the following aspect prints its name:

           #include <cstdio>
           aspect Tracing {
          // print the function name before execution starts
          advice execution ("% ...::%(...)") : before ()
                         std::printf ("in %s\n", JoinPoint::signature ());