Recipe19.4.Implementing the Mediator Pattern


Recipe 19.4. Implementing the Mediator Pattern

Problem

You want to apply the mediator pattern using AspectJ.

Solution

The mediator pattern allows the separation of potentially a large number of classes, that fit the colleague role, from each other by providing a single point of dependency in the mediator role. The class playing the mediator role minimizes dependencies between the colleague classes by providing a common point to control the different events that are initiated by the colleagues. The mediator accepts the events itself, and then encapsulates the logic that notifies the appropriate colleagues of the original event.

The abstract aspect in Example 19-7 uses the Director aspect-oriented design pattern (see Chapter 23) to provide the mechanisms by which the mediator pattern can be applied to an application.

Example 19-7. Defining the mediator pattern using aspects
public abstract aspect MediatorPattern  {    protected interface Colleague    {    }    protected interface Mediator    {    }    private WeakHashMap mappingColleagueToMediator = new WeakHashMap( );    private Mediator getMediator(Colleague colleague)    {       Mediator mediator =          (Mediator) mappingColleagueToMediator.get(colleague);       return mediator;    }    public void setMediator(Colleague c, Mediator m)    {       mappingColleagueToMediator.put(c, m);    }    protected abstract pointcut change(Colleague c);    after(Colleague c) : change(c)    {       notifyMediator(c, getMediator(c));    }    protected abstract void notifyMediator(Colleague c, Mediator m); }

Discussion

The MediatorPattern abstract aspect defines the Colleague and Mediator roles as interfaces that can be applied to application specific classes by inheriting subaspects. The aspect defines the mappingColleagueToMediator lookup which can be manipulated to assign colleague objects to mediator objects using the setMediator(Colleague,Mediator) method.

The MediatorPattern aspect provides the change(..) abstract pointcut that can be implemented by subaspects to trigger notifications on mediators when changes occur to colleagues by calling the notifyMediator(Colleague,Mediator) method that is also implemented by subaspects.

Figure 19-12 shows the structure of the MediatorPattern abstract aspect and the interfaces and behavior that it defines to support the mediator design pattern.

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


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

Example 19-8. Applying the MediatorPattern aspect to an application's classes
public aspect DialogMediator extends MediatorPattern  {    declare parents : ListBox implements Colleague;    declare parents : EntryField implements Mediator;        protected pointcut change(Colleague c) : (          execution(void ListBox.setSelection(..)) && this(c));        protected void notifyMediator(Colleague c, Mediator m)    {       ListBox listBox = (ListBox) c;       EntryField entryField = (EntryField) m;       entryField.setText(listBox.getSelection( ));    } }

Figure 19-13 shows the ListBox and EnTRyField classes before and after the DialogMediator aspect is applied.

Figure 19-13. The static structure after the mediator pattern has been applied to the ListBox and EntryField classes


Figure 19-14 shows how the mediator pattern characteristics of the ListBox and EntryField classes interact together within an example application.

Figure 19-14. Using the new mediator pattern behavior of the ListBox and EntryField classes


See Also

The Observer Pattern works in a more hierarchical manner than the Mediator pattern, but its implementation can be useful when comparing the two in terms of their appropriateness to a particular design problem. The Observer pattern as implemented using aspects is shown in Recipe Recipe 17.1; more information on the extension of existing classes using aspects can be found in Recipe Recipe 16.1; 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