笔记:Gof设计模式--Mediator
2012-06-02 18:01
232 查看
1、意图
Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring toeach other explicitly, and it lets you vary their interactionindependently.
2、适应性
Use the Mediator pattern when
• a set of objects communicate in well-defined but complex ways. Theresulting interdependencies are unstructured and difficult tounderstand.
• reusing an object is difficult because it refers to and communicateswith many other objects.
• a behavior that's distributed between several classes should becustomizable without a lot of subclassing.
3、结构
4、示例代码
We'll use a DialogDirector to implement the font dialog box shown inthe Motivation. The abstract class DialogDirector definesthe interface for directors.
Widget is the abstract base class for widgets. Awidget knows its director.
The ListBox, EntryField, and Button aresubclasses of Widget for specialized user interfaceelements. ListBox provides a GetSelectionoperation to get the current selection, and EntryField'sSetText operation puts new text into
the field.
The FontDialogDirector class mediates between widgets in thedialog box. FontDialogDirector is a subclass ofDialogDirector:
Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring toeach other explicitly, and it lets you vary their interactionindependently.
2、适应性
Use the Mediator pattern when
• a set of objects communicate in well-defined but complex ways. Theresulting interdependencies are unstructured and difficult tounderstand.
• reusing an object is difficult because it refers to and communicateswith many other objects.
• a behavior that's distributed between several classes should becustomizable without a lot of subclassing.
3、结构
4、示例代码
We'll use a DialogDirector to implement the font dialog box shown inthe Motivation. The abstract class DialogDirector definesthe interface for directors.
class DialogDirector { public: virtual ~DialogDirector(); virtual void ShowDialog(); virtual void WidgetChanged(Widget*) = 0; protected: DialogDirector(); virtual void CreateWidgets() = 0; };
Widget is the abstract base class for widgets. Awidget knows its director.
class Widget { public: Widget(DialogDirector*); virtual void Changed(); virtual void HandleMouse(MouseEv // ... private: DialogDirector* _director; }; // Widgets call WidgetChanged on their director toinform it of a significant event. void Widget::Changed () { _director->WidgetChanged(this); }
The ListBox, EntryField, and Button aresubclasses of Widget for specialized user interfaceelements. ListBox provides a GetSelectionoperation to get the current selection, and EntryField'sSetText operation puts new text into
the field.
class ListBox : public Widget { public: ListBox(DialogDirector*); virtual const char* GetSelection(); virtual void SetList(List<char*>* listItems); virtual void HandleMouse(MouseEvent& event); // ... }; class EntryField : public Widget { public: EntryField(DialogDirector*); virtual void SetText(const char* text); virtual const char* GetText(); virtual void HandleMouse(MouseEvent& event); // ... }; class Button : public Widget { public: Button(DialogDirector*); virtual void SetText(const char* text); virtual void HandleMouse(MouseEvent& event); // ... }; void Button::HandleMouse (MouseEvent& event) { // ... Changed(); }
The FontDialogDirector class mediates between widgets in thedialog box. FontDialogDirector is a subclass ofDialogDirector:
class FontDialogDirector : public DialogDirector { public: FontDialogDirector(); virtual ~FontDialogDirector(); virtual void WidgetChanged(Widget*); protected: virtual void CreateWidgets(); private: Button* _ok; Button* _cancel; ListBox* _fontList; EntryField* _fontName; }; // FontDialogDirector keeps track of the widgets it displays. // It redefines CreateWidgets to create the widgets and initialize itsreferences to // them: void FontDialogDirector::CreateWidgets () { _ok = new Button(this); _cancel = new Button(this); _fontList = new ListBox(this); _fontName = new EntryField(this); // fill the listBox with the available font names // assemble the widgets in the dialog } // WidgetChanged ensures that the widgets work together properly: void FontDialogDirector::WidgetChanged ( Widget* theChangedWidget ) { if (theChangedWidget == _fontList) { _fontName->SetText(_fontList->GetSelection()); } else if (theChangedWidget == _ok) { // apply font change and dismiss dialog // ... } else if (theChangedWidget == _cancel) { // dismiss dialog } }
相关文章推荐
- GoF设计模式学习笔记 第一回 Mediator
- 笔记:Gof设计模式--Singleton
- 笔记:Gof设计模式--Bridge
- GOF-23种设计模式——工厂模式(学习笔记)
- GoF设计模式学习笔记 前言
- GOF设计模式学习笔记 --辨析各种模式的要点和相似模式之间的区别
- GOF 设计模式(学习Hander First 设计模式 笔记)
- 设计模式学习笔记--中介者模式Mediator pattern(Java版)
- 【HeadFirst 设计模式学习笔记】17 中介者(Mediator)模式拾零
- GoF设计模式学习笔记 第二回 Iterator
- GOF-23种设计模式——单例模式(学习笔记)
- 步步为营 .NET 设计模式学习笔记 二十、Mediator(中介者模式)
- 笔记:Gof设计模式--Abstract Factory
- 【设计模式学习笔记十八】【行为模式】【中介者模式(Mediator)】
- 笔记--设计模式精解c++-GoF 23 种设计模式解析
- 笔记:Gof设计模式--Prototype
- 笔记--设计模式精解c++-GoF 23 种设计模式解析
- 设计模式(c++)笔记之十八(Mediator模式)
- 设计模式笔记(10 MEDIATOR & MEMENTO)
- 笔记:Gof设计模式--Proxy