中介者模式: 用一个中介者对象来封装一系列的对象交互,中介者使各对象不需要显示的交互引用,从而降低耦合;而且可以独立地改变它们之间的交互。
每个类都可以找中介者,每个类都持有中介者的引用(中介者是这些类的成员变量)。每个类都可以通过中介者找对象,让中介者干活。
适用于:用一个中介对象,封装一系列对象的交换。中介者使各个对象不需要显示的相互作用,从而实现了耦合松散,而且可以独立的改变他们之间的交换。
比如: 一个女人找对象,有多个男人,她需要和每个对象打交道,这样两个对象之间的耦合性太高了。如果通过婚姻介绍所,就降低了两个对象之间的耦合
不用中介者的代码:
class Person {
public:
Person (string name, int sex, int condi)
{
m_name = name;
m_sex = sex;
m_condi = condi;
}
string getName ()
{
return m_name;
}
int getSex ()
{
return m_sex;
}
int getCondi ()
{
return m_condi;
}
virtual void getPartner (Person *p) = 0;
protected:
string m_name;
int m_sex;
int m_condi;
};
class Woman : public Person {
public:
Woman(string name, int sex, int condi):Person(name,sex,condi) {}
virtual void getPartner (Person *p)
{
if (this->m_sex == p->getSex ())
{
cout << "不是同性恋..."<<endl;
}
else if (this->m_condi == p->getCondi ())
{
cout << this->m_name << "和"<<p->getName ()<<"绝配"<<endl;
}
else
{
cout << this->m_name << "和"<<p->getName ()<<"不配"<<endl;
}
}
};
class Man : public Person {
public:
Man(string name, int sex, int condi):Person(name,sex,condi) {}
virtual void getPartner (Person *p)
{
if (this->m_sex == p->getSex ())
{
cout << "不是同性恋..."<<endl;
}
else if (this->m_condi == p->getCondi ())
{
cout << this->m_name << "和"<<p->getName ()<<"绝配"<<endl;
}
else
{
cout << this->m_name << "和"<<p->getName ()<<"不配"<<endl;
}
}
};
int main()
{
Person *xiaofang = new Woman("小芳", 2, 5);
Person *zhangsan = new Man("张三", 1, 4);
Person *lisi = new Man("李四", 1, 5);
xiaofang->getPartner (zhangsan);
xiaofang->getPartner (lisi);
cout << "结束"<<endl;
system("pause");
return 0;
}
在这种场景下,引入一个中介者
class Person;
class Mediator
{
public:
void setMan(Person *man)
{
pMan = man;
}
void setWoman(Person *woman)
{
pWoman = woman;
}
public:
virtual void getParter()
{
if (pWoman->getSex () == pMan->getSex ())
{
cout << "同性恋 之间 不能找对象..."<<endl;
}
if (pWoman->getCondi () == pMan->getCondi ())
{
cout << pWoman->getName () << "和"<<pMan->getName ()<<"绝配"<<endl;
}
else
{
cout << pWoman->getName () << "和"<<pMan->getName ()<<"不配"<<endl;
}
}
private:
Person *pMan;
//list<pMan*> m_list; //多个对象
Person *pWoman;
};
class Person {
public:
Person (string name, int sex, int condi, Mediator *m)
{
m_name = name;
m_sex = sex;
m_condi = condi;
m_mediator = m;
}
string getName ()
{
return m_name;
}
int getSex ()
{
return m_sex;
}
int getCondi ()
{
return m_condi;
}
virtual void getPartner (Person *p) = 0;
protected:
string m_name;
int m_sex;
int m_condi;
Mediator *m_mediator;
};
class Woman : public Person {
public:
Woman(string name, int sex, int condi, Mediator *m):Person(name,sex,condi, m)
{
}
virtual void getPartner (Person *p)
{
m_mediator->setMan(p); //让中介者认识这个男人
m_mediator->setWoman(this); //让中介者认识自己
m_mediator->getPartner(); //让中介者找对象
}
};
class Man : public Person {
public:
Man(string name, int sex, int condi, Mediator *m):Person(name,sex,condi,man)
{
}
virtual void getPartner (Person *p)
{
m_mediator->setMan(this);
m_mediator->setWoman (p);
m_mediator->getPartner ();
}
};
int main()
{
Mediator *m = new Mediator;
Person *xiaofang = new Woman("小芳", 2, 5, m);
Person *zhangsan = new Man("张三", 1, 4, m);
Person *lisi = new Man("李四", 1, 5, m);
xiaofang->getPartner (zhangsan);
xiaofang->getPartner (lisi);
cout << "结束"<<endl;
system("pause");
return 0;
}