Recipe23.2.Applying the Director Design Pattern


Recipe 23.2. Applying the Director Design Pattern

Problem

You want to define a set of roles to be implemented by unknown sets of application classes so they can be interacted with generically by an abstract aspect.

Solution

Apply the Director aspect-oriented design pattern. Figure 23-2 shows the key components of the Director pattern.

Figure 23-2. The structure of the Director pattern


The key roles in the Director pattern shown in Figure 23-2 are:


DirectorAspect

The abstract aspect at the center of the design pattern that specifies the roles to be directed using Java interfaces and optionally specifies interactions that can occur between those roles.


Role1 and Role2

A pair of example roles defined as nested interfaces within the abstract DirectorAspect.


BusinessClassA and BusinessClassB

A pair of business classes within the target application that are candidates for the roles defined by the DirectorAspect.


SpecializedAspect

Applies the roles to specific classes within the target application. In this example, Role1 is applied to BusinessClassA and Role2 is applied to BusinessClassB. When BusinessClassB does not implement the bar( ) method necessary for the Role2 interface, this can be supplied using static cross-cutting techniques by the SpecializedAspect.

Discussion

The Director pattern decouples the generic and reusable aspect behavior from the implementation classes of a specific application, allowing the aspect's logic to direct the abstract roles rather than the implementations.

Example 23-2 is modified from Recipe 19.1 and shows how the Director pattern can be implemented in AspectJ to specify the Subject and Observer roles along with some default implementation details for the Subject role.

Example 23-2. An example of the Director pattern
public abstract aspect ObserverPattern  {    protected interface Subject    {       public void addObserver(Observer observer);       public void removeObserver(Observer observer);    }    protected interface Observer    {       public void notifyOfChange(Subject subject);    }    private List Subject.observers = new LinkedList( );    public void Subject.addObserver(Observer observer)    {       this.observers.add(observer);    }    public void Subject.removeObserver(Observer observer)    {       this.observers.remove(observer);    }    private synchronized void Subject.notifyObservers( )    {       Iterator iter = this.observers.iterator( );       while (iter.hasNext( ))       {          ((Observer)iter.next( )).notifyOfChange(this);       }    }    protected abstract pointcut subjectChange(Subject s);    after(Subject subject) : subjectChange(subject)    {       subject.notifyObservers( );    } }

Example 23-3, modified from Recipe 19.1, shows how the abstract ObserverPattern aspect uses the Director pattern to apply the Observer and Subject roles to the ConcreteClassA and ConcreteClassB target application classes.

Example 23-3. Applying the Director patterns roles to the target application's classes
public aspect ConcreteClassAObserver extends ObserverPattern  {    declare parents : ConcreteClassB implements Subject;    declare parents : ConcreteClassA implements Observer;    protected pointcut subjectChange(Subject s) :        call(* ConcreteClassB.set*(..))       && target(s);    public void ConcreteClassA.notifyOfChange(Subject subject)    {       this.doSomething(          "ConcreteClassA was notified of a change on " + subject);    } }

Here are the key characteristics of the Director pattern:

  • Provides a mechanism for declaring reusable aspect logic, including advice, and how it can interact with a set of abstract roles without having to know what target implementation classes will be coupled to those roles

  • Supports the encapsulation of the roles and the logic that works upon those roles in one place: the Director aspect

  • Allows you to declare logic that can be applied to entire families of target application classes

The Director pattern is useful in circumstances where you want to:

  • Define an abstract aspect without knowing what target application classes the aspect's logic will be applied to

  • Implement a set of relationships between abstract entities within your application, such as the implementation of an object-oriented design pattern

    Since the release of AspectJ 1.1, applying abstract roles as interfaces according to the Director pattern is the only way to introduce behavior across multiple classes as explained in the AspectJ documentation available at http://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectj-home/doc/README-11.html#SINGLE_INTERCLASS_TARGET.


See Also

Using abstract aspects and pointcuts and declaring the inheritance between aspects is covered in Chapter 15; the Director pattern is most prominently used throughout Chapter 17 through Chapter 19 when implementing the traditional object-oriented design patterns using aspect-oriented techniques.



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