首页 > 代码库 > 中介者模式

中介者模式

【1】什么是中介者模式?

中介者模式:

【2】中介者模式代码示例:

代码示例:

 1 #include<iostream> 2 #include <string> 3 using namespace std; 4  5 class Mediator; 6 class ConcreteColleague1; 7 class ConcreteColleague2; 8  9 class Colleague10 {11 protected:12     Mediator *mediator;13 public:14     Colleague(Mediator *mediator)15     {16         this->mediator = mediator;17     }18 };19 20 21 class ConcreteColleague1 : public Colleague22 {23 public:24     ConcreteColleague1(Mediator *mediator) : Colleague(mediator)25     {}26     void send(string message);27     void notify(string message)28     {29         cout << "同事1得到消息: " << message << endl;30     }31 };32 33 class ConcreteColleague2 : public Colleague34 {35 public:36     ConcreteColleague2(Mediator *mediator) : Colleague(mediator)37     {}38     void send(string message);39     void notify(string message)40     {41         cout << "同事2得到消息: " << message << endl;42     }43 };44 45 class Mediator46 {47 public:48     virtual void send(string name, Colleague *colleague)=0;49 };50 51 class ConcreteMediator : public Mediator52 {53 public:54     ConcreteColleague1 *colleague1;55     ConcreteColleague2 *colleague2;56 public:57     void send(string message, Colleague *colleague)58     {59         if (colleague == colleague1)60         {61             colleague2->notify(message);62         }63         else64         {65             colleague1->notify(message);66         }67     }68 };69 70   71 void ConcreteColleague1::send(string message) 72 {73     mediator->send(message, this);74 }75 76 void ConcreteColleague2::send(string message) 77 {78     mediator->send(message, this);79 }80 81 int main()82 {83     ConcreteMediator *m = new ConcreteMediator();84     ConcreteColleague1 *c1 = new ConcreteColleague1(m);85     ConcreteColleague2 *c2 = new ConcreteColleague2(m);86 87     m->colleague1 = c1;88     m->colleague2 = c2;89 90     c1->send("吃饭啦");91     c2->send("");92 93     return 0;94 }95 //Result:96 /*97 同事2得到消息: 吃饭啦98 同事1得到消息: 去99 */
View Code

代码示例2:

 1 #include<iostream> 2 #include <string> 3 using namespace std; 4  5 class Country; 6  7 class UniteNations 8 { 9 public:10     virtual void declare(string message, Country *colleague) = 0;11 }; 12 13 class Country14 {15 protected:16     UniteNations *mediator;17 public:18     Country(UniteNations *mediator)19     {20         this->mediator = mediator;21     }22 };23 24 class USA : public Country25 {26 public:27     USA(UniteNations *mediator) : Country(mediator)28     {}29     void declare(string message)30     {31         mediator->declare(message, this);32     }33     void getMessage(string message)34     {35         cout << "美国获得对方信息: " << message << endl;36     }37 };38 39 class Iraq : public Country40 {41 public:42     Iraq(UniteNations *mediator):Country(mediator)43     {}44     void declare(string message)45     {46         mediator->declare(message, this);47     }48     void getMessage(string message)49     {50         cout << "伊拉克获得对方信息: " << message << endl;51     }52 };53 54 class UnitedNationsSecurityCouncil:public UniteNations55 {56 public:57     USA *usa;58     Iraq *iraq;59     void declare(string message, Country *colleague)60     {61         if (colleague == usa)62         {63             iraq->getMessage(message);64         }65         else66         {67             usa->getMessage(message); 68         }69     }70 };71 72 int main()73 {74     UnitedNationsSecurityCouncil *unsc = new UnitedNationsSecurityCouncil();75     76     USA *c1 = new USA(unsc);77     Iraq *c2 = new Iraq(unsc);78 79     unsc->usa = c1;80     unsc->iraq = c2;81 82     c1->declare("不准开发核武器,否则打你!");83     c2->declare("他妈的美国去死!");84 85     return 0;86 }87 //Result:88 /*89 伊拉克获得对方信息: 不准开发核武器,否则打你!90 美国获得对方信息: 他妈的美国去死!91 */
View Code

 

Good   Good   Study,  Day   Day  Up.

顺序   选择   循环   总结

中介者模式