240 likes | 249 Views
Mediator. Matt G. Ellis. Overview. Intent Motivation Mediators in GUI applications Mediators and Relational Integrity Conclusion Questions. Intent.
E N D
Mediator Matt G. Ellis
Overview • Intent • Motivation • Mediators in GUI applications • Mediators and Relational Integrity • Conclusion • Questions
Intent • Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you very their interaction independently
Motivation • Object Oriented design encourages distribution of behavior among objects • However, Good design is thwarted by every object referencing every other object • Changing systems behavior becomes difficult • Helps to prevent classes from becoming “thick”
Mediator versus Façade • Façade pattern help refractor FlightPanel from Oozinoz • Refactoring can only go so far, complex applications still might need complex code even after applying Façade pattern
Mediators at Oozinoz • Chemicals for fireworks kept in tubs • Robots move most of the tubs from machine to machine • However, humans can override the system
FlightPanel_1 • Many methods exist to lazy-initialize variables • Rest control event handling logic
Challenge 1 • Refactor PlaceATub_1 into two classes, introducing a new PlaceATubMediator that receives the events of the GUI
Challenge 1 • Refactor PlaceATub_1 into two classes, introducing a new PlaceATubMediator that receives the events of the GUI
Relational Integrity • If Object A points to Object B then… • Object B points to Object A • A more rigorous definition can be found in Metsker, page 108
Relational Integrity and Java • Two Major Problems • Objects forget previous values • No built in support for Relational Integrity
Challenge 2 • Suppose we have this code: //tell tub about machine, and machine about tub t.setMachine(m); m.addTub(t); • What happens when t is tub T308 and m is Fuser-2101?
Challenge 2 • Really Bad Things… • Two machines think they have tub T308 in them • This can’t happen in the real world, why should it happen at Oozinoz? • Mediators can help
Mediators for Relational Integrity • Pull all relational information into a mediator outside both classes • Have both tubs and machines have a reference to this mediator • Use a Map to store these key/value pairs
Mediators for Relational Integrity • getMachine is simple, since t is the key of the map, HashMap makes it easy to get the value.
Mediators for Relational Integrity • Somewhat more complex, but the intent is the same.
Mediators for Relational Integrity • The most trivial method of all. Relational Integrity is maintained by the internal structure of the Map
Challenge 3 • Write the code for the Tub methods: getMachine() and setMachine()
Conclusions • Mediators provide loose coupling creating a “pluggable” system • Changing a mediator can change how applications deal with events • Mediators often found in GUIs • Swing’s event framework nudges the use of mediators, but they can be in the same class • Mediators also help to provide relational integrity between objects