Recipe19.3.Implementing the Iterator Pattern


Recipe 19.3. Implementing the Iterator Pattern

Problem

You want to apply the iterator pattern using AspectJ.

Solution

The iterator pattern provides a mechanism by which to separate the implementation of a collection of objects or an aggregate, as it is sometimes called, from the mechanisms by which the collection is sequentially accessed. The iterator, or cursor, is moved along the aggregate of the objects providing each while hiding the ordering and access implementation details from the users of the aggregate.

Example 19-5 uses the Director aspect-oriented design pattern (see Chapter 23) to provide an abstract aspect that can be used to apply the iterator pattern.

Example 19-5. Using aspects to define the iterator pattern
public abstract aspect IteratorPattern  {    public interface Aggregate    {       public Iterator createIterator( );              public Iterator createReverseIterator( );    } }

Discussion

The abstract IteratorPattern aspect declares the Aggregate role by defining the interface that an aggregate, meaning any collection of objects that can be iterated over, must fulfill. This interface includes two operations to create a normal or reverse iterator. The Java standard libraries make things less complicated here as the generic aspect can use the Iterator interface from the java.util package to meet the iterator role of the pattern.

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

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


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

Example 19-6. Specializing the abstract IteratorPattern aspect for a particular set of classes
public aspect EmployeeIteration extends IteratorPattern  {    declare parents : EmployeeCollection implements Aggregate;    public Iterator EmployeeCollection.createIterator( )    {       return new EmployeeIterator(this, true);    }    public Iterator EmployeeCollection.createReverseIterator( )    {       return new EmployeeIterator(this, false);    } }

The EmployeeIteration aspect, along with the supporting EmployeeIterator, declares that the EmployeeCollection is an aggregate and then implements the iterator creation methods for the collection.

Figure 19-10 shows the EmployeeCollection class before and after the EmployeeIteration aspect is applied.

Figure 19-10. The static structure after the iterator pattern has been applied to the EmployeeCollection class


Figure 19-11 shows how the new iterator pattern behavior of the EmployeeCollection class is interacted within an example application.

Figure 19-11. Using the new iterator pattern characteristics of the EmployeeCollection class


See Also

For more information on defining abstract aspects and specializing them, see Chapter 15; Chapter 16 contains 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