Upload
joan-mcdonald
View
89
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Design Pattern: Mediator. Modified from Kyle Kimport’s: Design Patterns: Mediator Ref: http://www.stevenblack.com. Outline:. What is a Mediator Why a Mediator? Anatomy of Mediator Consequences of Using a Mediator Mediator and Rest of the World - PowerPoint PPT Presentation
Citation preview
Design Pattern:Design Pattern:
MediatorMediator
Modified from Kyle Kimport’s: Modified from Kyle Kimport’s:
Design Patterns: MediatorDesign Patterns: Mediator
Ref: http://www.stevenblack.comRef: http://www.stevenblack.com
Outline:Outline: What is a MediatorWhat is a Mediator Why a Mediator?Why a Mediator? Anatomy of MediatorAnatomy of Mediator Consequences of Using a MediatorConsequences of Using a Mediator Mediator and Rest of the World Mediator and Rest of the World Implementing a MediatorImplementing a Mediator ConclusionsConclusions
What is a mediatorWhat is a mediator
• An object that abstracts inter-workings An object that abstracts inter-workings of two or more objects. of two or more objects.
• Acts as communication Acts as communication hubhub that serve that serve to decouple objects — no need to know to decouple objects — no need to know about each other, just need to know about each other, just need to know their Mediator.their Mediator.
• Promotes loose coupling by keeping Promotes loose coupling by keeping objects from referencing to each other objects from referencing to each other explicitlyexplicitly
Mediator:Mediator: a system of at least 3 objects messaging in a system of at least 3 objects messaging in a star topologya star topology
ColleagueColleague
ColleagueColleague
ColleagueColleague
MediatorMediator
OurMediatorOurMediator
OurMediatorOurMediator
OurMediatorOurMediator
Provides a common connection point, centralized Provides a common connection point, centralized (subclassable) behavior and behavior mgmt, all with a (subclassable) behavior and behavior mgmt, all with a common interfacecommon interface
Classification and IntentClassification and Intent
Classification: Object Behavior
Encapsulate object-to-object communication
Keeps objects from knowing about each other directly; this allows us easily change an object’s behavior
Motivation Motivation
OO-design allows for more reusable and OO-design allows for more reusable and more elegant programs, when objects more elegant programs, when objects need to refer to each other, this elegance need to refer to each other, this elegance is often lost.is often lost.
By consolidating all interaction in a single By consolidating all interaction in a single class, we can regain elegance and class, we can regain elegance and reusabilityreusability
When to use a mediator?When to use a mediator?
When one or more objects must interact When one or more objects must interact with several different objects.with several different objects.
When centralized control is desiredWhen centralized control is desired
When simple object need to communicate When simple object need to communicate in complex ways.in complex ways.
When you want to reuse an object that When you want to reuse an object that frequently interacts with other objectsfrequently interacts with other objects
StructureStructure
abstract Mediatorabstract Mediator abstract Collegueabstract Collegue
concrete Mediatorconcrete Mediator concrete Collegue 1concrete Collegue 1
concrete Collegue 2concrete Collegue 2
Participant--Abstract MediatorParticipant--Abstract Mediator
Define the Collegue-to-Mediator interfaceDefine the Collegue-to-Mediator interface
Participant--Concrete MediatorParticipant--Concrete Mediator Derivated from Abstract Mediator Derivated from Abstract Mediator
Aware of and knows the purpose of all concrete ColleguesAware of and knows the purpose of all concrete Collegues
Receives messages from a colleague & sends Receives messages from a colleague & sends
necessary commands to other colleaguesnecessary commands to other colleagues
Participant--Abstract ColleagueParticipant--Abstract ColleagueDefine the Mediator-to- Colleague interfaceDefine the Mediator-to- Colleague interface
Knows about the mediator, but none of its colleaguesKnows about the mediator, but none of its colleagues
Participant--Concrete ColleaguesParticipant--Concrete ColleaguesDerived from abstract Colleague Derived from abstract Colleague
Each Concrete Colleague knows its own behavior on a Each Concrete Colleague knows its own behavior on a small scale, but NOT on a large scalesmall scale, but NOT on a large scale..
Mediator: ProsMediator: Pros
Limits subclassing & specialization, allowing Limits subclassing & specialization, allowing Colleague classes to be reused w/o any changesColleague classes to be reused w/o any changes
Decouples colleagues, which facilitates independent Decouples colleagues, which facilitates independent variations of the colleague and mediator classes.variations of the colleague and mediator classes.
Simplifies protocol by replacing many-to-many Simplifies protocol by replacing many-to-many interaction with one-to-one interactioninteraction with one-to-one interaction
Abstracts object behaviors & cooperation. This allows Abstracts object behaviors & cooperation. This allows you to deal w/ an object’s small-scale behavior you to deal w/ an object’s small-scale behavior separately form its interaction with other objectsseparately form its interaction with other objects
Mediator: ConsMediator: Cons
Reducing the complexity of Colleagues increases the complexity of the Mediator itself. In some situations, maintaining a large Mediator may become as daunting a task as operating w/o a one.
Implementing a Mediator:Implementing a Mediator: Design ConsiderationDesign Consideration
When colleagues are general and When colleagues are general and reusable, we should use an abstract reusable, we should use an abstract MediatorMediator
When colleagues will not be used When colleagues will not be used elsewhere, you may forego the Abstract elsewhere, you may forego the Abstract MediatorMediator
Do we need an abstract Mediator?Do we need an abstract Mediator?
How Colleagues Interact w/ the Mediator?How Colleagues Interact w/ the Mediator?
Implement the mediator itself as an ObserverImplement the mediator itself as an Observer
Colleague pass themselves as arguments Colleague pass themselves as arguments when communicating with the mediatorwhen communicating with the mediator
Mediator & the Rest of the WorldMediator & the Rest of the World
Observer: the Mediator class may Observer: the Mediator class may be implemented using an Observerbe implemented using an Observer
Façade: is similar to a Mediator, Façade: is similar to a Mediator, but with one-way communication but with one-way communication from the Façade to its subsystem from the Façade to its subsystem classes.classes.
Related Design PatternRelated Design Pattern
Sample Java CodeSample Java Code
abstract class abstract class MediatorMediator{{
public abstract void colleagueChanged(Colleague c);public abstract void colleagueChanged(Colleague c);
}}
abstract class abstract class ColleagueColleague{{
private Mediator myMediator;private Mediator myMediator;
public void changed(){public void changed(){
myMediator.colleagueChanged(this);myMediator.colleagueChanged(this);
}}
}}
class ListBox extends class ListBox extends ColleagueColleague{{
public void display(){public void display(){
//..drawing routines//..drawing routines
}}
public void mouseClick(int x, int y){public void mouseClick(int x, int y){
//ListBox obj does sth…//ListBox obj does sth…
}}
}}
Sample Java CodeSample Java Code
class FontDialogBox extends class FontDialogBox extends MediatorMediator{{
private Button ok; private List fontList; private TextField fontName;
private CheckBox latino; private CheckBOx latin2; //...more //...more
public FontDialogBOx(){public FontDialogBOx(){
fontList = new ListBox(this); fontName = new TextField(this); //...fontList = new ListBox(this); fontName = new TextField(this); //...
}}
public void display(){public void display(){
fontList.display();fontList.display();
fontName.display(); //...fontName.display(); //...
}}
public void colleagueChanged(Colleague c){public void colleagueChanged(Colleague c){
if( c==fontList)if( c==fontList)
//font_list does sth…//font_list does sth…
else if(c==fontName)else if(c==fontName)
//fontName does sth.//fontName does sth.
else if……else if……
}}
}}
}}
Declare ColleagueDeclare Colleague ObjectsObjects
Manipulate colleague Manipulate colleague ObjectsObjects
Mediator ObjectMediator Object
ConclusionConclusionA mediator is an objcet-behavior design pattern.A mediator is an objcet-behavior design pattern.
Use a Mediator when simple objects interact in Use a Mediator when simple objects interact in complex wayscomplex ways
The Mediator pattern consists of two types of The Mediator pattern consists of two types of objects: the Mediator and its Colleaguesobjects: the Mediator and its Colleagues
All object-to-object communication is All object-to-object communication is encapsulated in the Mediatorencapsulated in the Mediator
Mediator allows for greater reusability, and Mediator allows for greater reusability, and generally more elegant, readable code.generally more elegant, readable code.