O'Reilly logo

AspectJ Cookbook by Russ Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

21.3. Applying Lazy Loading

Problem

You want to apply lazy loading techniques to a component of your application.

Solution

Use the Director aspect-oriented design pattern to set up the roles involved in lazy loading as interfaces inside an abstract aspect, as shown in Example 21-6.

Example 21-6. Using the Director aspect-oriented pattern to define the roles that take part in lazy loading

public abstract aspect LazyLoading extends DelegatingProxyPattern
{
   public interface RealComponent extends Subject
   {
   }
   
   public interface LazyProxy extends RealComponent
   {
      public RealComponent getRealComponent( ) throws LazyLoadingException;
   }
   
   public abstract LazyProxy initializeComponent(Object configuration);
}

The LazyLoading aspect in turn inherits from an implementation of the Proxy pattern, as shown in Recipe 14.6, that focuses on the delegation characteristics of the Proxy pattern, as shown in Example 21-7.

Example 21-7. Specializing the ProxyPattern behavior to focus on delegation

public abstract aspect DelegatingProxyPattern extends ProxyPattern 
{
   protected boolean reject(
      Object caller,
      Subject subject,
      JoinPoint joinPoint)
   {
      return false;
   }

   protected boolean delegate(
      Object caller,
      Subject subject,
      JoinPoint joinPoint)
   {
      return true;
   }

   protected Object rejectRequest(
      Object caller,
      Subject subject,
      JoinPoint joinPoint)
   {
      return null;
   }
}

Finally, create specialized subaspects of the LazyLoading aspect that will implement the lazy loading behavior for specific components of your target application. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required