Aspect-Oriented Programming
Core Concerns vs Cross-Cutting Concerns
A core concern is a unit of work that contributes to the business logic of an
application, such as storing records in a database or generating reports. A cross-
cutting concern is a feature that spans across core concerns and supports
an application, such as logging or caching.
Aspect-Oriented Programming
Aspect-oriented programming (AOP) isolates cross-cutting concerns through the
use of aspects. Aspects work by targeting a method, called a join point,
and surrounding it with additional code. Join points are determined by an
expression called a pointcut. For example, in order to find the slowest method
in an application, you could declare a pointcut that targets all public methods and
compare the time before and after each join point executes.
Proxy Objects
A simple way to implement aspect-oriented programming is through the use
of proxy objects. A proxy is a wrapper that provides additional functionality
to an object of interest. Proxies can be implemented through interfaces
or inheritance.
Interface Proxies
An object that implements an interface can be proxied by another class that
implements the same interface. For example, in order to log every method call
on an ArrayList, you could create a ProxyList class that implements
List
and delegates every method to a private ArrayList. Each delegated
method provides a join point that can be surrounded by logging code.
This proxy could be passed to any method that accepts a List. However,
one limitation of interface proxies is that any self-invoked method inside the
composed object will not be proxied. For example, if the remove() method
in ArrayList internally calls the get() method, that call will not be logged
because the join point will never be invoked.
|