Recipe19.6.Implementing the Memento Pattern


Recipe 19.6. Implementing the Memento Pattern

Problem

You want to apply the memento pattern using AspectJ.

Solution

The memento pattern provides a mechanism by which an object's original state can be reinstated at a later time without coupling the exact mechanisms with which the state of the object is rolled back to the object. The memento encapsulates all of the information needed to restore a prior internal state of an object at a later date. This capability can be used to provide a form of undo feature to the state of objects within a particular application.

The abstract aspect and support classes in Example 19-11 use the Director aspect-oriented design pattern (see Chapter 23) to specify the mechanisms by which the memento pattern can be applied to an application using AspectJ.

Example 19-11. Defining the memento pattern using aspects
public abstract aspect MementoPattern  {    public interface Memento    {       public void setState(Originator originator);       public Object getState( );    }        public interface Originator    {       public void setMemento(Memento memento);       public Memento createMemento( );       public Object getState( );    }

Discussion

The MementoPattern aspect defines the roles and behavior of memento and originator objects according to the design pattern. Those roles can be applied to the application specific classes by specialized subaspects.

Figure 19-19 shows the structure of the MementoPattern abstract aspect and the interfaces and behavior that it defines to support the memento design pattern.

Figure 19-19. The MementoPattern aspect and the interfaces it defines for the design pattern's roles


Example 19-12 shows how the abstract MementoPattern aspect could be applied for a specific application.

Example 19-12. Applying the MementoPattern aspect to an application's classes
public aspect EmployeeMemento extends MementoPattern  {    declare parents : Employee implements Originator;    public void Employee.setMemento(Memento memento)    {       Object object = memento.getState( );       Employee stateToRestore = (Employee) object;       this.setName(stateToRestore.getName( ));       this.setSalary(stateToRestore.getSalary( ));    }    public Memento Employee.createMemento( )    {       Memento memento = new DefaultMemento( );       memento.setState(this);       return memento;    }    public Object Employee.getState( ) throws MementoException    {       Employee employee = new Employee(this.getName( ), this.getSalary( ));       return employee;    } }

The EmployeeMemento specifies that the Employee class is an originator and so supports mementos of its state being created. To completely support this new role, the Employee class is extended with the createMemento(), setMemento(Memento), and getState( ) methods.

The createMemento( ) method allows a client to get a memento for an Employee object, the setMemento(Memento) method will restore an Employee object to the state stored in a memento, and the getState( ) method is used by a memento when it is being created to access and store the state of the Employee originator object.

Figure 19-20 shows the effects before and after applying the EmployeeMemento aspect to the Employee class.

Figure 19-20. The static structure before and after the memento pattern has been applied to the Employee class


Figure 19-21 shows an example interaction with the Employee class using the aspect introduced memento pattern features.

Figure 19-21. Using the new memento pattern characteristics of the Employee class


See Also

The recipes in Chapter 16 contain more details on the mechanisms by which existing classes can be extended using aspects and the declare keyword; the Director aspect-oriented design pattern is explained in Recipe 23.3.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net