Recipe19.1.Implementing the Observer Pattern


Recipe 19.1. Implementing the Observer Pattern

Problem

You want to apply the observer pattern using AspectJ.

Solution

The observer pattern allows the designer to create dependencies between objects such that if one object's state changes the other objects will be notified and may act accordingly.

Example 19-1 uses the Director aspect-oriented design pattern (see Chapter 23) to provide a generic aspect implementation of the observer pattern.

Example 19-1. Defining the observer pattern using an abstract aspect
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( );    } }

Discussion

The ObserverPattern aspect defines the Subject and Observer roles as interfaces that can be applied to specific classes by inheriting subaspects. With these roles defined, the remainder of the ObserverPattern aspect implements the mechanism by which to notify the observers when a subject changes.

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

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


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

Example 19-2. Applying the ObserverPattern to an 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);    } }

The ConcreteClassAObserver applies the Observer interface to the ConcreteClassA class and the Subject interface to the ConcreteClassB class. This means ConcreteClassA objects that are registered as observers are notified when a modifier is called on a ConcreteClassB object. The specifics of what methods are called when a notification is to be made are declared in the notifyOfChange(Subject) method added to the ConcreteClassA class.

Figure 19-2 shows an example set of application classes before the ConcreteClassAOb-server aspect is applied.

Figure 19-2. The ConcreteClassA and ConcreteClassB classes


Figure 19-3 shows the effects after applying the ConcreteClassAObserver aspect to the ConcreteClassA and ConcreteClassB classes.

Figure 19-3. The static structure after the observer pattern has been applied to the ConcreteClassA and ConcreteClassB classes


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 call(Signature) pointcut is covered in Recipe Recipe 4.1; the execution(Signature) pointcut is described in Recipe 4.4; exposing join point context is examined in Recipe 13.2; the target(Type or Identifier) pointcut is examined in Recipe 11.2; the Director aspect-oriented design pattern is explained in Recipe 23.3.

Figure 19-4 shows how the new observer behavior of the ConcreteClassA class and the subject behavior of the ConcreteClassB class is interacted within an example application.

Figure 19-4. Using the observer and subject behavior of the ConcreteClassA and ConcreteClassB classes




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