中介者模式
1. 中介者模式:
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示的相互引用,从而使其耦合松散。
优缺点:
中介者模式很容易在系统中应用,也很容易误用。当系统出现了‘多对多’交互复杂的对象群时,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。
优点:
减少了Colleague的耦合,使得可以独立改变和复用各个Colleague类和Mediator,比如任何国家改变不影响其他国家,只与安理会发生变化。
缺点:
使得中介者变得比任何一个ConcreteColleague都更复杂。
2. 中介者模式:
Colleague叫做抽象同事类,而ConcreteColleague是具体同事类,每个具体同事只知道自己的行为,而不了解其他同事类的情况,但他们都认识中介者对象,Mediator是抽象中介者,定义了同事对象到中介者对象的接口,ConcreteMediator是具体中介者对象,实现抽象类方法,它需要知道所有具体同事类,并从具体同事接收消息,向具体同事对象发出命令。
#include<iostream>
#include <string>
using namespace std;
class Mediator;
class ConcreteColleague1;
class ConcreteColleague2;
class Colleague//抽象同事类
{
protected:
Mediator *mediator;//聚合关系------中介者对象
public:
Colleague(Mediator *mediator)
{
this->mediator=mediator;
}
};
class ConcreteColleague1:public Colleague
{
public:
ConcreteColleague1(Mediator *mediator):Colleague(mediator)
{}
void send(string message);
void notify(string message)
{
cout<<"同事得到消息: "<<message<<endl;
}
};
class ConcreteColleague2:public Colleague
{
public:
ConcreteColleague2(Mediator *mediator):Colleague(mediator)
{}
void send(string message);
void notify(string message)
{
cout<<"同事得到消息: "<<message<<endl;
}
};
class Mediator//抽象中介者
{
public:
virtual void send(string name,Colleague *colleague)=0;
};
class ConcreteMediator:public Mediator
{
public:
ConcreteColleague1 *colleague1;//需要包含所有的具体同事对象
ConcreteColleague2 *colleague2;
public:
void send(string message,Colleague *colleague)
{
if(colleague==colleague1)
{
colleague2->notify(message);
}
else
{
colleague1->notify(message);
}
}
};
void ConcreteColleague1::send(string message)
{
mediator->send(message,this);
}
void ConcreteColleague2::send(string message)
{
mediator->send(message,this);
}
int main()
{
ConcreteMediator *m=new ConcreteMediator();
//让两个具体同事认识中介者对象
ConcreteColleague1 *c1=new ConcreteColleague1(m);
ConcreteColleague2 *c2=new ConcreteColleague2(m);
//让中介者认识各个具体同事类对象
m->colleague1=c1;
m->colleague2=c2;
c1->send("吃饭啦");
c2->send("去");
return 0;
}
3. 中介者模式的例子:
联合国机构类相当于Mediator中介者类;
国家类,相当于Colleague同事类;
美国类,相当于ConcreteColleague具体同事类;
#include<iostream>
#include <string>
using namespace std;
class Country;
//联合国机构
class UniteNations
{
public:
virtual void declare(string message,Country *colleague)=0;
};
//国家
class Country
{
protected:
UniteNations *mediator;
public:
Country(UniteNations *mediator)
{
this->mediator=mediator;
}
};
//美国类
class USA:public Country
{
public:
USA(UniteNations *mediator):Country(mediator)
{}
void declare(string message)
{
mediator->declare(message,this);
}
void getMessage(string message)
{
cout<<"美国获得对方信息: "<<message<<endl;
}
};
//伊拉克类
class Iraq:public Country
{
public:
Iraq(UniteNations *mediator):Country(mediator)
{}
void declare(string message)
{
mediator->declare(message,this);
}
void getMessage(string message)
{
cout<<"伊拉克获得对方信息: "<<message<<endl;
}
};
//联合国安全理事会
class UnitedNationsSecurityCouncil:public UniteNations
{
public:
USA *usa;
Iraq *iraq;
void declare(string message,Country *colleague)
{
if(colleague==usa)
{
iraq->getMessage(message);
}
else
{
usa->getMessage(message);
}
}
};
int main()
{
UnitedNationsSecurityCouncil *unsc=new UnitedNationsSecurityCouncil();
USA *c1=new USA(unsc);
Iraq *c2=new Iraq(unsc);
unsc->usa=c1;
unsc->iraq=c2;
c1->declare("不准不听话!");
c2->declare("我的地盘我做主!");
return 0;
}