Aspect-Oriented Programming (AOP)
  • is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns
  • it does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a “pointcut” specification
  • such as “log all function calls when the function’s name begins with ‘set’”
  • this allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the business code

The drawback of Object-Oriented Programming (OOP)

Let’s say you want to have a behavior that crosses several classes. For example, log files or a monitoring routine. This routine would need to run on many classes. We’d have to add the monitor to almost every class or method! This type of behavior crosscuts a wide array of classes, much like a diagonal street. OOP comes up short in this aspect

AOP helps to provide a solution to the previous problem. Instead of objects, AOP deals with aspects. An aspect is a behavior that cuts through multiple objects.

One of the drawbacks to OOP is the failure to crosscut objects and methods. That is, a single behavior will apply to multiple classes. An example is logging or monitoring; you would want to monitor/log updates to several classes in a financial program. In OOP, you have to add the logging to each class or method. AOP avoids this by treating this crosscutting behavior as an aspect, as opposed to an object.

These aspects are defined separately from the main code. When you want to run the logging routine, you set a pointcut or place it in the code where you run the code. This could be in a class or method. By having a single block of code to deal with the aspect, you greatly reduce the redundancy that comes with crosscutting behavior.

Terminology

  • crosscut -
  • aspect - a modularization of a concern that cuts across multiple objects. Each aspect focuses on a specific crosscutting functionality
  • join-point - a point during the execution of a script, such as the execution of a method or property access
  • advice - action taken by an aspect at a particular join point
  • pointcut - regular expression that matches join points. An advice is associated with a pointcut expression and runs at any join point that matches the pointcut
  • weaving - linking aspects with other application types or objects to create an advised object

Weaving Types

  • Compile-Time Weaving (CTW) - weaving done during the compilation phase
  • Load Time Weaving (LTW) - weaving done when the class is loaded within the JVM
  • Runtime Weaving - weaving done during runtime