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

            積木

            No sub title

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

            常用鏈接

            留言簿(1)

            我參與的團(tuán)隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉(zhuǎn)載自: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.

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

            依賴關(guān)系的轉(zhuǎn)化示例:

            20. C++實現(xiàn)Behavioral - Mediator模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

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

            2. 共有10個關(guān)系需要維護(hù); 2. 共有5個關(guān)系需要維護(hù);

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

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

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

            4. 如果要增加一個新類F,那么需要 4. 如果要增加一個類F,那么要維護(hù)的關(guān)系

            維護(hù)的關(guān)系數(shù)量將變?yōu)?/span>15,并且 數(shù)量將變?yōu)?/span>6,原有的5個類不需要做任

            原有的5個類均需要做相應(yīng)改變。 和改變,僅需改變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.

            業(yè)務(wù)示例:假定有如下圖所示的界面:

            20. C++實現(xiàn)Behavioral - Mediator模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

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

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

            還有一個假設(shè):

            CheckBoxesComboBox分別只能選擇其中一項(復(fù)選框也不能復(fù)選,與其實際功能不符,純?yōu)檠芯坑?/span>)

            優(yōu)點:

            - 將多對多的關(guān)系轉(zhuǎn)化為一對多的關(guān)系,使對象之間的關(guān)系更易于維護(hù);

            - 將對象的行為和協(xié)作抽象化。

            缺點:

            - 雖然降低了同事類的復(fù)雜性,但增加了調(diào)停者對象的復(fù)雜性;

            - 同事類的復(fù)用性是以調(diào)停者類的不可復(fù)用為代價的。

            C++實現(xiàn)代碼:

            // Mediator.h

            #include <string>

            #include <iostream>

            using namespace std;

            // 前置聲明

            class AbstractColleague;

            // 抽象調(diào)停類

            class AbstractMediator

            {

            public:

            // 通知"同事"類,其參數(shù)aac為引發(fā)事件的對象

            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);

            }

            };

            // 復(fù)選框

            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);

            }

            };

            // 具體調(diào)停類

            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事件觸發(fā),并選中第個單選按鈕

            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事件觸發(fā),并選中第個復(fù)選框

            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事件觸發(fā),并選中第個選項

            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;

            }

            運行結(jié)果:

            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++實現(xiàn)Behavioral - Mediator模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客



            posted on 2013-03-08 14:33 Jacc.Kim 閱讀(190) 評論(0)  編輯 收藏 引用 所屬分類: 設(shè)計模式
            日韩欧美亚洲综合久久影院d3| 国产高潮国产高潮久久久| 国产高潮国产高潮久久久91| 国产三级观看久久| 午夜视频久久久久一区| 亚洲精品乱码久久久久久久久久久久 | 国产成人精品久久一区二区三区| 久久人爽人人爽人人片AV| 国内精品久久久久久麻豆| 久久精品国产清自在天天线| 91精品国产乱码久久久久久| 久久亚洲国产成人精品无码区| 久久久久亚洲精品天堂| 香蕉久久AⅤ一区二区三区| 99久久人妻无码精品系列 | av无码久久久久久不卡网站| 久久精品国产国产精品四凭| 99久久人妻无码精品系列| 2020久久精品亚洲热综合一本| 天天爽天天爽天天片a久久网| 色综合久久综合中文综合网| 色婷婷久久久SWAG精品| 久久综合狠狠色综合伊人| 久久w5ww成w人免费| 精品综合久久久久久97| 亚洲精品无码久久久久AV麻豆| 99久久免费国产特黄| 人妻精品久久无码区| 久久精品中文无码资源站| 久久亚洲国产最新网站| 日韩电影久久久被窝网| 99久久久久| 欧美粉嫩小泬久久久久久久 | 久久精品无码专区免费青青| 99久久精品免费看国产一区二区三区 | 久久精品无码一区二区日韩AV| 国产一久久香蕉国产线看观看 | 色播久久人人爽人人爽人人片AV| 久久亚洲中文字幕精品一区| 久久亚洲高清综合| 精品久久久久久久久免费影院 |