青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

積木

No sub title

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用鏈接

留言簿(1)

我參與的團隊

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

轉載自:http://patmusing.blog.163.com/blog/static/1358349602010150177996/

“Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction” – GoF

用一個中介對象來封裝一系列的對象交互。Mediator使各對象不需要顯示的相互引用,從而使其耦合松散,以便可以獨立地改變他們之間的交互。

Object-oriented design encourages the distribution of behavior among objects. Such distribution can result in an object structure with many connections between objects; in the worst case, every object ends up knowing about every other.

Though partitioning a system into many objects generally enhances reusability, proliferating interconnections tend to reduce it again. Lots of interconnections make it less likely that an object can work without the support of others—the system acts as though it were monolithic. Moreover, it can be difficult to change the system's behavior in any significant way, since behavior is distributed among many objects. As a result, you may be forced to define many subclasses to customize the system's behavior.

在軟件構建過程中,經常會出現多個對象相互關聯交互的情況,對象之間常常會維持一種復雜的引用關系,如果遇到一些需求的更改,這種直接的引用關系將面臨不斷的變化。在這種情況下,我們可以使用一個中介對象來管理對象間的關聯關系,避免相互交互的對象之間的緊耦合的引用關系,從而更好地駕馭變化。

依賴關系的轉化示例:

20. C++實現Behavioral - Mediator模式 - 玄機逸士 - 玄機逸士博客

1. 5個類相互之間兩兩相互依賴; 1. 5各類彼此之間沒有直接的依賴關系;

2. 共有10個關系需要維護; 2. 共有5個關系需要維護;

3. 如果任意其中一個類發生了改 3. 如果任意其中一個類發生了改變,那么

變,那么另外4個類,度需要 只需要修改Mediator即可,其它4

隨之改變; 類可以維持不變;

4. 如果要增加一個新類F,那么需要 4. 如果要增加一個類F,那么要維護的關系

維護的關系數量將變為15,并且 數量將變為6,原有的5個類不需要做任

原有的5個類均需要做相應改變。 和改變,僅需改變Mediator即可。

Applicability:

- A set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructed and difficult to understand.

- Resuing an object is difficult because it refers to and communicates with many other objects.

- A behavior that’s distributed between several classes should be customizable without a lot of subclassing.

Participants:

- Mediator:

Defines an interface for communicating with Colleague objects.

- ConcreteMediator:

Implements cooperative behavior by coordinatiing Colleague objects.

Knows and maintains its colleagues.

- Colleagues

Each Colleague class knows its Mediator object.

Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.

業務示例:假定有如下圖所示的界面:

20. C++實現Behavioral - Mediator模式 - 玄機逸士 - 玄機逸士博客

RadioButtons包含了4個單選,CheckBoxes包含了4個復選框,ComboBox中有4個選項。先假定:

如果RadioButtons中選擇了Roption1,那么CheckBoxes中的Coption1就會被選中,ComboBox中的ComOption1就會被選中;如果CheckBoxes中的COption1被選中,那么RadioButtons中的ROption1ComboBox中的ComOption1就會被選中;如果ComboBox中的ComOption1被選中,那么RadioButtons中的ROption1CheckBoxes中的Coption1就會被選中。以此類推。另外在這里,RadioButtons和其中的單選按鈕被看成是一個對象,4個單選按鈕可視為RadioButtons4個狀態;CheckBoxesComboBox也是如此。

還有一個假設:

CheckBoxesComboBox分別只能選擇其中一項(復選框也不能復選,與其實際功能不符,純為研究用)

優點:

- 將多對多的關系轉化為一對多的關系,使對象之間的關系更易于維護;

- 將對象的行為和協作抽象化。

缺點:

- 雖然降低了同事類的復雜性,但增加了調停者對象的復雜性;

- 同事類的復用性是以調停者類的不可復用為代價的。

C++實現代碼:

// Mediator.h

#include <string>

#include <iostream>

using namespace std;

// 前置聲明

class AbstractColleague;

// 抽象調停類

class AbstractMediator

{

public:

// 通知"同事"類,其參數aac為引發事件的對象

virtual void notify_colleagues(AbstractColleague *aac) = 0;

public:

virtual ~AbstractMediator()

{

cout << "in the destructor of AbstractMediator..." << endl;

}

};

// 抽象"同事"

class AbstractColleague

{

protected:

AbstractMediator *mediator;

public:

AbstractColleague(AbstractMediator *mediator)

{

this->mediator = mediator;

}

~AbstractColleague()

{

cout << "in the destructor of AbstractColleague..." << endl;

}

public:

virtual void set_item_true(int i) = 0;

virtual int get_true_item() = 0;

void on_change(AbstractColleague *aac)

{

mediator->notify_colleagues(aac);

}

};

// 單選按鈕

class RadioButtons : public AbstractColleague

{

private:

bool rButton1;

bool rButton2;

bool rButton3;

bool rButton4;

public:

RadioButtons(AbstractMediator *mediator) : AbstractColleague(mediator)

{

rButton1 = false;

rButton2 = false;

rButton3 = false;

rButton4 = false;

}

~RadioButtons()

{

cout << "in the destructor of RadioButtons..." << endl;

}

public:

void set_item_true(int i)

{

rButton1 = false;

rButton2 = false;

rButton3 = false;

rButton4 = false;

switch(i)

{

case 1:

rButton1 = true;

break;

case 2:

rButton2 = true;

break;

case 3:

rButton3 = true;

break;

case 4:

rButton4 = true;

break;

default:

rButton1 = true;

}

}

int get_true_item()

{

if(rButton1) return 1;

if(rButton2) return 2;

if(rButton3) return 3;

if(rButton4) return 4;

return 1;

}

void onRadioButtonClick()

{

on_change(this);

}

};

// 復選框

class CheckBoxes : public AbstractColleague

{

private:

bool cBox1;

bool cBox2;

bool cBox3;

bool cBox4;

public:

CheckBoxes(AbstractMediator *mediator) : AbstractColleague(mediator)

{

cBox1 = false;

cBox2 = false;

cBox3 = false;

cBox4 = false;

}

~CheckBoxes()

{

cout << "in the destructor of CheckBoxes..." << endl;

}

public:

void set_item_true(int i)

{

cBox1 = false;

cBox2 = false;

cBox3 = false;

cBox4 = false;

switch(i)

{

case 1:

cBox1 = true;

break;

case 2:

cBox2 = true;

break;

case 3:

cBox3 = true;

break;

case 4:

cBox4 = true;

break;

default:

cBox1 = true;

}

}

int get_true_item()

{

if(cBox1) return 1;

if(cBox2) return 2;

if(cBox3) return 3;

if(cBox4) return 4;

return 1;

}

void onCheckBoxClick()

{

on_change(this);

}

};

// 下拉框

class ComboBox : public AbstractColleague

{

private:

bool cOpt1;

bool cOpt2;

bool cOpt3;

bool cOpt4;

public:

ComboBox(AbstractMediator *mediator) : AbstractColleague(mediator)

{

cOpt1 = false;

cOpt2 = false;

cOpt3 = false;

cOpt4 = false;

}

~ComboBox()

{

cout << "in the destructor of ComboxBox..." << endl;

}

public:

void set_item_true(int i)

{

cOpt1 = false;

cOpt2 = false;

cOpt3 = false;

cOpt4 = false;

switch(i)

{

case 1:

cOpt1 = true;

break;

case 2:

cOpt2 = true;

break;

case 3:

cOpt3 = true;

break;

case 4:

cOpt4 = true;

break;

default:

cOpt1 = true;

}

}

int get_true_item()

{

if(cOpt1) return 1;

if(cOpt2) return 2;

if(cOpt3) return 3;

if(cOpt4) return 4;

return 1;

}

void onComboBoxClick()

{

on_change(this);

}

};

// 具體調停類

class ConcreteMediator : public AbstractMediator

{

private:

RadioButtons* rbt;

CheckBoxes* cbx;

ComboBox* cbo;

public:

~ConcreteMediator()

{

cout << "in the destructor of ConcreteMediator..." << endl;

}

void set_colleagues(RadioButtons* rbt, CheckBoxes* cbx, ComboBox* cbo)

{

this->rbt = rbt;

this->cbx = cbx;

this->cbo = cbo;

}

void notify_colleagues(AbstractColleague *aac)

{

int i = aac->get_true_item();

rbt->set_item_true(i);

cbx->set_item_true(i);

cbo->set_item_true(i);

}

};

// Mediator.cpp

#include "Mediator.h"

int main(int argc, char **argv)

{

AbstractMediator* mediator = new ConcreteMediator();

RadioButtons* rbt = new RadioButtons(mediator);

CheckBoxes* cbx = new CheckBoxes(mediator);

ComboBox* cbo = new ComboBox(mediator);

dynamic_cast<ConcreteMediator*>(mediator)->set_colleagues(rbt, cbx, cbo);

// 下面兩行模擬RadioButtonsonClick事件觸發,并選中第個單選按鈕

rbt->set_item_true(1);

rbt->onRadioButtonClick();

cout << "Event triggered by the No.1 item of RadioButtons" << endl;

cout << "rButton" << rbt->get_true_item() << " \tis selected!" << endl;

cout << "cBox" << cbx->get_true_item() << " \t\tis selected accordingly!" << endl;

cout << "cOpt" << cbo->get_true_item() << " \t\tis selected accordingly!" << endl;

cout << "------------------------------------------------------" << endl;

// 下面兩行模擬CheckBoxesonClick事件觸發,并選中第個復選框

cbx->set_item_true(2);

cbx->onCheckBoxClick();

cout << "Event triggered by the No.2 item of CheckBoxes" << endl;

cout << "rButton" << rbt->get_true_item() << " \tis selected accordingly!" << endl;

cout << "cBox" << cbx->get_true_item() << " \t\tis selected!" << endl;

cout << "cOpt" << cbo->get_true_item() << " \t\tis selected accordingly!" << endl;

cout << "------------------------------------------------------" << endl;

// 下面兩行模擬ComboBoxonClick事件觸發,并選中第個選項

cbo->set_item_true(3);

cbo->onComboBoxClick();

cout << "Event triggered by the No.3 item of ComboBox" << endl;

cout << "rButton" << rbt->get_true_item() << " \tis selected accordingly!" << endl;

cout << "cBox" << cbx->get_true_item() << " \t\tis selected accordingly!" << endl;

cout << "cOpt" << cbo->get_true_item() << " \t\tis selected!" << endl;

cout << "------------------------------------------------------" << endl;

delete mediator;

delete rbt;

delete cbx;

delete cbo;

return 0;

}

運行結果:

Event triggered by the No.1 item of RadioButtons

rButton1 is selected!

cBox1 is selected accordingly!

cOpt1 is selected accordingly!

------------------------------------------------------

Event triggered by the No.2 item of CheckBoxes

rButton2 is selected accordingly!

cBox2 is selected!

cOpt2 is selected accordingly!

------------------------------------------------------

Event triggered by the No.3 item of ComboBox

rButton3 is selected accordingly!

cBox3 is selected accordingly!

cOpt3 is selected!

------------------------------------------------------

in the destructor of ConcreteMediator...

in the destructor of AbstractMediator...

in the destructor of RadioButtons...

in the destructor of AbstractColleague...

in the destructor of CheckBoxes...

in the destructor of AbstractColleague...

in the destructor of ComboxBox...

in the destructor of AbstractColleague...

上述程序的UML類圖:

20. C++實現Behavioral - Mediator模式 - 玄機逸士 - 玄機逸士博客



posted on 2013-03-08 14:33 Jacc.Kim 閱讀(196) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            乱码第一页成人| 亚洲视频在线看| 欧美日本二区| 久久精品视频免费| 一二美女精品欧洲| 亚洲久久一区二区| 老牛影视一区二区三区| 亚洲欧美在线高清| aⅴ色国产欧美| 亚洲电影下载| 国产精品美女久久福利网站| 亚洲欧洲午夜| 久久一区二区三区国产精品| 亚洲欧美影院| 欧美一区二区三区播放老司机| 久久精品道一区二区三区| 亚洲第一黄网| 亚洲网站视频福利| 欧美淫片网站| 欧美日韩国产区一| 国产一区二区0| 亚洲精选在线观看| 亚洲国产精品久久久久婷婷884 | 在线视频日韩精品| 一本色道久久加勒比精品 | 欧美大片在线观看一区二区| 欧美影院在线| 久久久www成人免费毛片麻豆| 久久久久久久久久久久久女国产乱| 欧美gay视频| 在线观看免费视频综合| 久久久国产精品一区二区三区| 日韩一级片网址| 欧美高清自拍一区| 在线日韩中文字幕| 欧美在线观看一区二区| 91久久午夜| 欧美精品亚洲精品| 亚洲大片一区二区三区| 性欧美18~19sex高清播放| 亚洲欧洲日本在线| 老牛影视一区二区三区| 在线观看日韩av| 久久乐国产精品| 久久国产精品久久久| 欧美日韩在线一区二区| 久久免费一区| 亚洲电影在线看| 嫩草伊人久久精品少妇av杨幂| 亚洲欧美日本日韩| 欧美日韩视频免费播放| 亚洲国产精品成人精品| 亚洲国产精品欧美一二99| 欧美一级黄色网| 蜜桃av一区二区| 国产日韩欧美综合一区| 老色批av在线精品| 欧美日韩在线一区| 亚洲欧洲日产国产网站| 国产原创一区二区| 久久国产毛片| 亚洲男人天堂2024| 农村妇女精品| 欧美一区二区观看视频| 免费在线观看一区二区| 久久精品国产亚洲高清剧情介绍| 狂野欧美激情性xxxx| 欧美福利视频一区| 艳女tv在线观看国产一区| 久久蜜臀精品av| 久久综合亚洲社区| 激情婷婷亚洲| 欧美精品一区二区视频| 亚洲精品国久久99热| 亚洲欧洲精品一区二区精品久久久| 久久深夜福利免费观看| 亚洲国产欧美在线人成| 一区二区三区欧美在线| 久久久久亚洲综合| 久久香蕉国产线看观看av| 国产精品麻豆va在线播放| 亚洲久久一区| 一区二区三区在线免费视频| 禁久久精品乱码| 亚洲日韩成人| 国一区二区在线观看| 亚洲免费视频一区二区| 欧美日韩精品一区二区三区| 欧美xxx成人| 亚洲精品国产系列| 99热免费精品在线观看| 亚洲欧美日本在线| 国产精品视区| 噜噜噜噜噜久久久久久91| 亚洲免费高清视频| 欧美成人午夜视频| 亚洲日本aⅴ片在线观看香蕉| 国产精品老牛| 蜜桃av久久久亚洲精品| 亚洲天堂av电影| 亚洲理伦电影| 亚洲国产另类久久精品| 国产一区二区精品久久91| 久久这里有精品15一区二区三区| 亚洲欧洲另类| 亚洲日本欧美天堂| 欧美岛国在线观看| 美女国内精品自产拍在线播放| 中文av一区特黄| 午夜精品一区二区三区四区| 在线视频中文亚洲| 亚洲视频中文| 99热这里只有成人精品国产| 黄页网站一区| 国产午夜久久| 欧美日韩一区二区三区在线| 性欧美videos另类喷潮| 亚洲一区在线看| 亚洲一级高清| 欧美一区综合| 免费成人av在线看| 久久亚洲一区二区三区四区| 国产精品久久久一区麻豆最新章节 | 亚洲尤物在线视频观看| 亚洲人成人99网站| 亚洲激情国产| 亚洲国产日韩欧美在线99| 国产婷婷色一区二区三区| 欧美大色视频| 欧美在线播放| 欧美自拍偷拍| 久久综合狠狠综合久久激情| 久久成人精品| 亚洲国产一二三| 欧美在线免费观看视频| 欧美www视频| 国产精一区二区三区| 最新成人在线| 久久天堂国产精品| 一区二区精品| 欧美风情在线| 国产午夜精品久久| 一区二区免费看| 久久久av水蜜桃| 亚洲美女免费视频| 久久精彩免费视频| 久久综合狠狠| 国产精品久久久久久一区二区三区 | 欧美日韩1080p| 影音先锋国产精品| 欧美一二三视频| 国产精品99久久99久久久二8| 久久成人免费网| 国产午夜精品全部视频播放| 韩国视频理论视频久久| 亚洲图片欧美一区| 久久久精品国产99久久精品芒果| 亚洲国产精品一区二区www在线| 亚洲高清网站| 欧美激情一区二区三区在线视频 | 亚洲成人在线| 日韩午夜在线视频| 国产精品日韩在线| 欧美国产精品| 国产精品在线看| 99re6这里只有精品视频在线观看| 国产欧美一区二区精品仙草咪| 女女同性女同一区二区三区91| 久久午夜色播影院免费高清| 在线中文字幕一区| 亚洲一区二区三区在线播放| 欧美精品一卡| 久久精品理论片| 国产精品美女午夜av| 亚洲欧美制服另类日韩| 久久久久久欧美| 亚洲伦伦在线| 国产精品久久久爽爽爽麻豆色哟哟| 亚洲免费在线精品一区| 免费成人高清视频| 亚洲私人影院| 亚洲春色另类小说| 欧美三级电影网| 久久夜色精品一区| 亚洲图片自拍偷拍| 亚洲第一级黄色片| 香蕉久久夜色精品| 亚洲国产视频直播| 国产精品有限公司| 欧美精品亚洲精品| 久久人人爽人人爽爽久久| 亚洲视频1区| 欧美激情国产日韩| 久久精品夜夜夜夜久久| 亚洲专区一区二区三区| 日韩一区二区精品葵司在线| 精品白丝av| 国产日韩欧美一二三区| 欧美日韩亚洲系列| 欧美精品激情blacked18|