笔记:Gof设计模式--Mediator

 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.

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 
    } 
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值