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

20.3. Adding Persistence to a Class

Problem

You want to add persistence to a class so it can be saved and restored.

Solution

Apply the Director aspect-oriented design pattern and create an abstract aspect that defines the roles and behavior required to manage object persistence, as shown in Example 20-6.

This abstract aspect can be extended into specialized subaspects that will implement an appropriate persistence mechanism for each collection of persisted objects within your application.

Example 20-6. Defining the generic roles and behavior of persistence in an abstract aspect

public abstract aspect PersistenceAspect 
{
   public interface ObjectStore
   {
      public void persist( );
      public void restore( );
   }
   
   protected abstract pointcut restoreStorage(ObjectStore store);
   
   after(ObjectStore store) : restoreStorage(store)
   {
      store.restore( );
   }
   
   protected abstract pointcut persistStorage(ObjectStore store);
   
   after(ObjectStore store) : persistStorage(store)
   {  
      store.persist( );
   }
}

Discussion

The abstract PersistenceAspect aspect defines the ObjectStore role as an interface that can be applied to any class that will manage the persistence of a collection of objects. The restoreStorage(ObjectStore) and persistStorage(ObjectStore) abstract pointcuts are implemented by specialized subaspects to trigger the corresponding after( ) advice blocks that will restore or persist the specified ObjectStore.

The restore( ) and persist( ) methods specified in the ObjectStore interface are implemented according to ...

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