import java.util.WeakHashMap; import java.util.List; import java.util.LinkedList; import java.util.Iterator; /** * Defines the general behavior of the observer design pattern. * * Each concrete sub-aspect of ObserverProtocol defines one kind of observing * relationship. Within that kind of relationship, there can be any number * of subjects, each with any number of observers. * * The sub-aspect defines three things:
    * *
  1. what types can be subjects or observers
    * this is done using +implements * *
  2. what operations on the subject require updating the observers
    * this is done by concretizing the changes(Subject) pointcut * *
  3. how to update the observers
    * this is done by defining a method on * updateObserver(Subject, Observer) *
* * Note that in this implementation, the work of updating is a method * on the sub-aspect, not a method introduced on the observer. This * allows one class of object to be the observer in different kinds of * observing relationships, each of which has a different updating * behavior. For observers that just have a single generic update * behavior, the method on updateObserver will just be a simple call * that generic updater. * * @author Gregor Kiczales * @author Jan Hannemann * @version 1.0, 05/13/02 * */ public abstract aspect ObserverProtocol { /** * This interface is used by extending aspects to say what types * can be subjects. It models the subject role. */ protected interface Subject { } /** * This interface is used by extending aspects to say what types * can be observers. It models the observer role. */ protected interface Observer { } /** * Stores the mapping between Subjects and * Observers. For each subject, a LinkedList * is of its observers is stored. */ private WeakHashMap perSubjectObservers; /** * Returns a Collection of the observers of * a particular subject. Used internally. * * @param s the subject for which to return the observers * @return a Collection of s's observers */ protected List getObservers(Subject s) { if (perSubjectObservers == null) { perSubjectObservers = new WeakHashMap(); } List observers = (List)perSubjectObservers.get(s); if ( observers == null ) { observers = new LinkedList(); perSubjectObservers.put(s, observers); } return observers; } /** * Adds an observer to a subject. This is the equivalent of * attach(), but is a method on the pattern aspect, not the subject. * * @param s the subject to attach a new observer to * @param o the new observer to attach */ public void addObserver(Subject s, Observer o) { getObservers(s).add(o); } /** * Removes an observer from a subject. This is the equivalent of * detach(), but is a method on the pattern aspect, not the subject. * * @param s the subject to remove the observer from * @param o the observer to remove */ public void removeObserver(Subject s, Observer o) { getObservers(s).remove(o); } /** * The join points after which to do the update. * It replaces the normally scattered calls to notify(). To be * concretized by sub-aspects. */ protected abstract pointcut subjectChange(Subject s); /** * Call updateObserver after a change of interest to update each observer. * * @param s the subject on which the change occured */ after(Subject s): subjectChange(s) { Iterator iter = getObservers(s).iterator(); while ( iter.hasNext() ) { updateObserver(s, ((Observer)iter.next())); } } /** * Defines how each Observer is to be updated when a change * to a Subject occurs. To be concretized by sub-aspects. * * @param s the subject on which a change of interest occured * @param o the observer to be notifed of the change */ protected abstract void updateObserver(Subject s, Observer o); }