design pattern: mediator mediator modified from kyle kimport’s: design patterns: mediator design...

Download Design Pattern: Mediator Mediator Modified from Kyle Kimport’s: Design Patterns: Mediator Design Patterns: Mediator Ref: http://www.stevenblack.com

Post on 18-Dec-2015

214 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Design Pattern: Mediator Mediator Modified from Kyle Kimports: Design Patterns: Mediator Design Patterns: Mediator Ref: http://www.stevenblack.com
  • Slide 2
  • Outline: What is a Mediator What is a Mediator Why a Mediator? Why a Mediator? Anatomy of Mediator Anatomy of Mediator Consequences of Using a Mediator Consequences of Using a Mediator Mediator and Rest of the World Mediator and Rest of the World Implementing a Mediator Implementing a Mediator Conclusions Conclusions
  • Slide 3
  • What is a mediator An object that abstracts inter-workings of two or more objects. An object that abstracts inter-workings of two or more objects. Acts as communication hub that serve to decouple objects no need to know about each other, just need to know their Mediator. Acts as communication hub that serve to decouple objects no need to know about each other, just need to know their Mediator. Promotes loose coupling by keeping objects from referencing to each other explicitly Promotes loose coupling by keeping objects from referencing to each other explicitly
  • Slide 4
  • Mediator: a system of at least 3 objects messaging in a star topology Colleague Colleague Colleague Mediator OurMediator OurMediator OurMediator Provides a common connection point, centralized (subclassable) behavior and behavior mgmt, all with a common interface
  • Slide 5
  • Classification and Intent Classification: Object Behavior Encapsulate object-to-object communication Keeps objects from knowing about each other directly; this allows us easily change an objects behavior
  • Slide 6
  • Motivation OO-design allows for more reusable and more elegant programs, when objects need to refer to each other, this elegance is often lost. OO-design allows for more reusable and more elegant programs, when objects need to refer to each other, this elegance is often lost. By consolidating all interaction in a single class, we can regain elegance and reusability By consolidating all interaction in a single class, we can regain elegance and reusability
  • Slide 7
  • When to use a mediator? When one or more objects must interact with several different objects. When one or more objects must interact with several different objects. When centralized control is desired When centralized control is desired When simple object need to communicate in complex ways. When simple object need to communicate in complex ways. When you want to reuse an object that frequently interacts with other objects When you want to reuse an object that frequently interacts with other objects
  • Slide 8
  • Structure abstract Mediator abstract Collegue concrete Mediator concrete Collegue 1 concrete Collegue 2
  • Slide 9
  • Participant--Abstract Mediator Define the Collegue-to-Mediator interface Define the Collegue-to-Mediator interface Participant--Concrete Mediator Derivated from Abstract Mediator Derivated from Abstract Mediator Aware of and knows the purpose of all concrete Collegues Aware 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 colleagues necessary commands to other colleagues
  • Slide 10
  • Participant--Abstract Colleague Define the Mediator-to- Colleague interface Define the Mediator-to- Colleague interface Knows about the mediator, but none of its colleagues Knows about the mediator, but none of its colleagues Participant--Concrete Colleagues Derived from abstract Colleague Derived from abstract Colleague Each Concrete Colleague knows its own behavior on a small scale, but NOT on a large scale. Each Concrete Colleague knows its own behavior on a small scale, but NOT on a large scale.
  • Slide 11
  • Mediator: Pros Limits subclassing & specialization, allowing Colleague classes to be reused w/o any changes Limits subclassing & specialization, allowing Colleague classes to be reused w/o any changes Decouples colleagues, which facilitates independent variations of the colleague and mediator classes. Decouples colleagues, which facilitates independent variations of the colleague and mediator classes. Simplifies protocol by replacing many-to-many interaction with one-to-one interaction Simplifies protocol by replacing many-to-many interaction with one-to-one interaction Abstracts object behaviors & cooperation. This allows you to deal w/ an objects small-scale behavior separately form its interaction with other objects Abstracts object behaviors & cooperation. This allows you to deal w/ an objects small-scale behavior separately form its interaction with other objects
  • Slide 12
  • Mediator: 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.
  • Slide 13
  • Implementing a Mediator: Design Consideration When colleagues are general and reusable, we should use an abstract Mediator When colleagues are general and reusable, we should use an abstract Mediator When colleagues will not be used elsewhere, you may forego the Abstract Mediator When colleagues will not be used elsewhere, you may forego the Abstract Mediator Do we need an abstract Mediator?
  • Slide 14
  • How Colleagues Interact w/ the Mediator? Implement the mediator itself as an Observer Implement the mediator itself as an Observer Colleague pass themselves as arguments when communicating with the mediator Colleague pass themselves as arguments when communicating with the mediator
  • Slide 15
  • Mediator & the Rest of the World Observer: the Mediator class may be implemented using an Observer Observer: the Mediator class may be implemented using an Observer Faade: is similar to a Mediator, but with one-way communication from the Faade to its subsystem classes. Faade: is similar to a Mediator, but with one-way communication from the Faade to its subsystem classes. Related Design Pattern
  • Slide 16
  • Mediator: Real World Example
  • Slide 17
  • Sample Java Code abstract class Mediator { public abstract void colleagueChanged(Colleague c); public abstract void colleagueChanged(Colleague c);} abstract class Colleague{ private Mediator myMediator; private Mediator myMediator; public void changed(){ public void changed(){ myMediator.colleagueChanged(this); myMediator.colleagueChanged(this); }} class ListBox extends Colleague{ 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 }}
  • Slide 18
  • Sample Java Code class FontDialogBox extends Mediator{ private Button ok; private List fontList; private TextField fontName; //...more private CheckBox latino; private CheckBOx latin2; //...more public FontDialogBOx(){ fontList = new ListBox(this); fontName = new TextField(this); //... fontList = new ListBox(this); fontName = new TextField(this); //...} public void display(){ fontList.display(); fontList.display(); fontName.display(); //... fontName.display(); //...} 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 Colleague Objects Objects Manipulate colleague Objects Mediator Object
  • Slide 19
  • Conclusion A mediator is an objcet-behavior design pattern. A mediator is an objcet-behavior design pattern. Use a Mediator when simple objects interact in complex ways Use a Mediator when simple objects interact in complex ways The Mediator pattern consists of two types of objects: the Mediator and its Colleagues The Mediator pattern consists of two types of objects: the Mediator and its Colleagues All object-to-object communication is encapsulated in the Mediator All object-to-object communication is encapsulated in the Mediator Mediator allows for greater reusability, and generally more elegant, readable code. Mediator allows for greater reusability, and generally more elegant, readable code.

Recommended

View more >