Top Banner
Mediator Mediator Matt G. Ellis Matt G. Ellis
24

Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

MediatorMediator

Matt G. EllisMatt G. Ellis

Page 2: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

OverviewOverview

► IntentIntent►MotivationMotivation►Mediators in GUI applicationsMediators in GUI applications►Mediators and Relational IntegrityMediators and Relational Integrity►ConclusionConclusion►QuestionsQuestions

Page 3: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 4: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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”

Page 5: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 6: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 7: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 8: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 9: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 10: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 11: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 12: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 13: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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?

Page 14: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

Challenge 2Challenge 2

Page 15: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

Challenge 2Challenge 2

Page 16: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

Challenge 2Challenge 2

Page 17: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 18: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 19: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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.

Page 20: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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.

Page 21: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 22: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

Challenge 3Challenge 3

►Write the code for the Tub methods: Write the code for the Tub methods: getMachine() and setMachine()getMachine() and setMachine()

Page 23: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

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

Page 24: Mediator Matt G. Ellis. Overview ► Intent ► Motivation ► Mediators in GUI applications ► Mediators and Relational Integrity ► Conclusion ► Questions.

QuestionsQuestions