Recipe19.8.Implementing the Visitor Pattern


Recipe 19.8. Implementing the Visitor Pattern

Problem

You want to apply the visitor pattern using AspectJ.

Solution

The visitor pattern encapsulates a request that can be executed by a hierarchy of objects as it is passed throughout the structure. The abstract aspect in Example 19-15 uses the Director aspect-oriented design pattern (see Chapter 23) to define the roles that take part in the Visitor pattern.

Example 19-15. Defining the visitor pattern using aspects
public abstract aspect VisitorPattern  {    public interface Element    {       public void accept(Visitor visitor);    }    public interface CompositeElement extends Element    {       public Element[] getElements( );    }    public interface Result    {    }    public interface Visitor    {       public void visitElement(Element element);       public void visitComposite(CompositeElement element);       public Result getResult( );    }    public void CompositeElement.accept(Visitor visitor)    {       visitor.visitComposite(this);    }    public void Element.accept(Visitor visitor)    {       visitor.visitElement(this);    } }

Discussion

The VisitorPattern abstract aspect defines the CompositeElement and Element roles as parts of the object structure that is to be visited. The Visitor role describes how the Visitor is notified of which type of element it is visiting. This role is applied to objects that may be passed to the different parts of the structure, be they composite or simple elements. The CompositeElement and Element roles are then extended to provide the methods by which the Visitor is passed.

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

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


Example 19-16 shows how the abstract VisitorPattern aspect could be applied to a specific application.

Example 19-16. Applying the VisitorPattern aspect to an application
public aspect InventoryVisitor extends VisitorPattern  {         declare parents : FloppyDisk implements Element;         declare parents : HardDisk implements Element;         declare parents : Processor implements Element;         declare parents : Computer implements CompositeElement;         declare parents : Motherboard implements CompositeElement;         declare parents : InventoryReport implements Result;         public Element[] Computer.getElements( )         {                 Element[] elements = new Element[3];                 elements[0] = this.getMotherboard( );                 elements[1] = this.getHardDisk( );                 elements[2] = this.getFloppyDisk( );                 return elements;         }         public Element[] Motherboard.getElements( )         {                 Element[] elements = new Element[1];                 elements[0] = this.getProcessor( );                 return elements;         } }

Figure 19-27 shows an example set of application classes before the InventoryVisitor aspect is applied.

Figure 19-27. The relationships between classes that represent the components of a computer


Figure 19-28 shows the effects of applying the InventoryVisitor aspect to the application classes shown in Figure 19-27.

Figure 19-28. The computer part classes with the new visitor pattern interfaces applied


Figure 19-29 shows a partial example interaction with some of the classes in Figure 19-28 using the aspect introduced visitor pattern features. The interactions recursively continue throughout all of the composite elements and individual elements, calling accept(Visitor) and then getdescription( ) on each, that make up the top-level Computer object.

Figure 19-29. A partial interaction with the visitor pattern characteristics of the Computer, Motherboard, and InventoryReportVisitor classes


See Also

The visitor pattern is often used together with the composite pattern shown in Recipe 18.1; For more information on defining abstract aspects and specializing them, please refer to the recipes in Chapter 15; 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