1 / 19

Design Pattern: Mediator

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

Télécharger la présentation

Design Pattern: Mediator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline: • What is a Mediator • Why a Mediator? • Anatomy of Mediator • Consequences of Using a Mediator • Mediator and Rest of the World • Implementing a Mediator • Conclusions

  3. What is a mediator • 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. • Promotes loose coupling by keeping objects from referencing to each other explicitly

  4. Mediator:a system of at least 3 objects messaging in a star topology Colleague OurMediator Mediator Colleague OurMediator Colleague OurMediator Provides a common connection point, centralized (subclassable) behavior and behavior mgmt, all with a common interface

  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 object’s behavior

  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. • By consolidating all interaction in a single class, we can regain elegance and reusability

  7. When to use a mediator? • When one or more objects must interact with several different objects. • When centralized control is desired • When simple object need to communicate in complex ways. • When you want to reuse an object that frequently interacts with other objects

  8. Structure abstract Mediator abstract Collegue concrete Mediator concrete Collegue 1 concrete Collegue 2

  9. Participant--Abstract Mediator Define the Collegue-to-Mediator interface Participant--Concrete Mediator • Derivated from Abstract Mediator • Aware of and knows the purpose of all concrete Collegues • Receives messages from a colleague & sends necessary commands to other colleagues

  10. Participant--Abstract Colleague • Define the Mediator-to- Colleague interface • Knows about the mediator, but none of its colleagues Participant--Concrete Colleagues • Derived from abstract Colleague • Each Concrete Colleague knows its own behavior on a small scale, but NOT on a large scale.

  11. Mediator: Pros • 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. • Simplifies protocol by replacing many-to-many interaction with one-to-one interaction • Abstracts object behaviors & cooperation. This allows you to deal w/ an object’s small-scale behavior separately form its interaction with other objects

  12. Mediator: Cons Reducing the complexity of Colleaguesincreases 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.

  13. Implementing a Mediator:Design Consideration • 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 Do we need an abstract Mediator?

  14. How Colleagues Interact w/ the Mediator? • Implement the mediator itself as an Observer • Colleague pass themselves as arguments when communicating with the mediator

  15. Mediator & the Rest of the World Related Design Pattern • Observer: the Mediator class may be implemented using an Observer • Façade: is similar to a Mediator, but with one-way communication from the Façade to its subsystem classes.

  16. Mediator: Real World Example

  17. Sample Java Code abstract class Mediator{ public abstract void colleagueChanged(Colleague c); } abstract class Colleague{ private Mediator myMediator; public void changed(){ myMediator.colleagueChanged(this); } } class ListBox extends Colleague{ public void display(){ //..drawing routines } public void mouseClick(int x, int y){ //ListBox obj does sth… } }

  18. Mediator Object Sample Java Code class FontDialogBox extends Mediator{ private Button ok; private List fontList; private TextField fontName; private CheckBox latino; private CheckBOx latin2; //...more public FontDialogBOx(){ fontList = new ListBox(this); fontName = new TextField(this); //... } public void display(){ fontList.display(); fontName.display(); //... } public void colleagueChanged(Colleague c){ if( c==fontList) //font_list does sth… else if(c==fontName) //fontName does sth. else if…… } } } Declare Colleague Objects Manipulate colleague Objects

  19. Conclusion • A mediator is an objcet-behavior design pattern. • Use a Mediator when simple objects interact in complex ways • The Mediator pattern consists of two types of objects: the Mediator and its Colleagues • All object-to-object communication is encapsulated in the Mediator • Mediator allows for greater reusability, and generally more elegant, readable code.

More Related