Design Patterns Java™ Workbook

(Michael S) #1
Chapter 10. Mediator

public Machine getMachine(Tub t)
{
return (Machine) tubToMachine.get(t);
}


public Set getTubs(Machine m)
{
Set set = new HashSet();
Iterator i = tubToMachine.entrySet().iterator();
while (i.hasNext())
{
Map.Entry e = (Map.Entry) i.next();
if (e.getValue().equals(m))
{
set.add(e.getKey());
}
}
return set;
}


public void set(Tub t, Machine m)
{
tubToMachine.put(t, m);
}
}


Figure 10.6. The Mediator pattern lets you define an object that encapsulates how tubs and
machines interact.


CHALLENGE 10.4


Write the code for the Tub methods getMachine() and setMachine().
Free download pdf