mediator matt g. ellis. overview ► intent ► motivation ► mediators in gui applications ►...
Post on 19-Dec-2015
216 views
TRANSCRIPT
MediatorMediator
Matt G. EllisMatt G. Ellis
OverviewOverview
► IntentIntent►MotivationMotivation►Mediators in GUI applicationsMediators in GUI applications►Mediators and Relational IntegrityMediators and Relational Integrity►ConclusionConclusion►QuestionsQuestions
IntentIntent
►Define an object that encapsulates Define an object that encapsulates how a set of objects interact. Mediator how a set of objects interact. Mediator promotes loose coupling by keeping promotes loose coupling by keeping objects from referring to each other objects from referring to each other explicitly, and it lets you very their explicitly, and it lets you very their interaction independentlyinteraction independently
MotivationMotivation
►Object Oriented design encourages Object Oriented design encourages distribution of behavior among objectsdistribution of behavior among objects
►However, Good design is thwarted by However, Good design is thwarted by every object referencing every other every object referencing every other objectobject
►Changing systems behavior becomes Changing systems behavior becomes difficultdifficult
►Helps to prevent classes from becoming Helps to prevent classes from becoming “thick”“thick”
Mediator versus FaçadeMediator versus Façade
►Façade pattern help refractor Façade pattern help refractor FlightPanel from OozinozFlightPanel from Oozinoz
►Refactoring can only go so far, Refactoring can only go so far, complex applications still might need complex applications still might need complex code even after applying complex code even after applying Façade patternFaçade pattern
Mediators at OozinozMediators at Oozinoz
► Chemicals for Chemicals for fireworks kept in fireworks kept in tubstubs
► Robots move most Robots move most of the tubs from of the tubs from machine to machinemachine to machine
►However, humans However, humans can override the can override the system system
FlightPanel_1FlightPanel_1
►Many methods exist Many methods exist to lazy-initialize to lazy-initialize variables variables
► Rest control event Rest control event handling logichandling logic
Challenge 1Challenge 1
► Refactor PlaceATub_1 into two classes, Refactor PlaceATub_1 into two classes, introducing a new PlaceATubMediator that introducing a new PlaceATubMediator that receives the events of the GUIreceives the events of the GUI
Challenge 1Challenge 1
► Refactor Refactor PlaceATub_1 into PlaceATub_1 into two classes, two classes, introducing a new introducing a new PlaceATubMediator PlaceATubMediator that receives the that receives the events of the GUIevents of the GUI
Relational IntegrityRelational Integrity
► If Object A points to Object B then…If Object A points to Object B then…►Object B points to Object AObject B points to Object A►A more rigorous definition can be A more rigorous definition can be
found in Metsker, page 108 found in Metsker, page 108
Relational Integrity and JavaRelational Integrity and Java
► Two Major Two Major ProblemsProblems Objects forget Objects forget
previous valuesprevious values No built in support No built in support
for Relational for Relational IntegrityIntegrity
TubTub MachineMachine
T30T3055
StarPress-2402StarPress-2402
T30T3088
StarPress-2402StarPress-2402
T37T3777
ShellAssembler-ShellAssembler-23012301
T37T3799
ShellAssembler-ShellAssembler-23012301
T38T3899
ShellAssembler-ShellAssembler-23012301
T00T0011
Fuser-2102Fuser-2102
T00T0022
Fuser-2102Fuser-2102
ModelModel
TubTub MachineMachine
T30T3055
StarPress-2402StarPress-2402
T30T3088
StarPress-2402StarPress-2402
T37T3777
ShellAssembler-ShellAssembler-23012301
T37T3799
ShellAssembler-ShellAssembler-23012301
T38T3899
ShellAssembler-ShellAssembler-23012301
T00T0011
Fuser-2102Fuser-2102
T00T0022
Fuser-2102Fuser-2102
Challenge 2Challenge 2
►Suppose we have this code:Suppose we have this code://tell tub about machine, and machine about //tell tub about machine, and machine about tubtub
t.setMachine(m);t.setMachine(m);
m.addTub(t);m.addTub(t);
►What happens when t is tub T308 and What happens when t is tub T308 and m is Fuser-2101?m is Fuser-2101?
Challenge 2Challenge 2
Challenge 2Challenge 2
Challenge 2Challenge 2
Challenge 2Challenge 2
► Really Bad Things…Really Bad Things…► Two machines think Two machines think
they have tub T308 they have tub T308 in themin them
► This can’t happen in This can’t happen in the real world, why the real world, why should it happen at should it happen at Oozinoz?Oozinoz?
►Mediators can helpMediators can help
Mediators for Relational Mediators for Relational IntegrityIntegrity
►Pull all relational information into a Pull all relational information into a mediator outside both classesmediator outside both classes
►Have both tubs and machines have a Have both tubs and machines have a reference to this mediatorreference to this mediator
►Use a Map to store these key/value Use a Map to store these key/value pairspairs
Mediators for Relational Mediators for Relational IntegrityIntegrity
► getMachine is simple, since t is the key of the map, getMachine is simple, since t is the key of the map, HashMap makes it easy to get the value.HashMap makes it easy to get the value.
Mediators for Relational Mediators for Relational IntegrityIntegrity
► Somewhat more complex, but the intent is Somewhat more complex, but the intent is the same.the same.
Mediators for Relational Mediators for Relational IntegrityIntegrity
► The most trivial method of all. Relational The most trivial method of all. Relational Integrity is maintained by the internal Integrity is maintained by the internal structure of the Mapstructure of the Map
Challenge 3Challenge 3
►Write the code for the Tub methods: Write the code for the Tub methods: getMachine() and setMachine()getMachine() and setMachine()
ConclusionsConclusions
►Mediators provide loose coupling Mediators provide loose coupling creating a “pluggable” systemcreating a “pluggable” system Changing a mediator can change how Changing a mediator can change how
applications deal with eventsapplications deal with events►Mediators often found in GUIsMediators often found in GUIs
Swing’s event framework nudges the use Swing’s event framework nudges the use of mediators, but they can be in the same of mediators, but they can be in the same classclass
►Mediators also help to provide Mediators also help to provide relational integrity between objectsrelational integrity between objects
QuestionsQuestions