定义:
用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之前的交互。
主要解决
对象与对象之前存在大量的关联关系,这样势必会造成系统变得复杂,若一个对象改变,我们常常需要跟踪与之关联的对象,并做出相应的处理。
如何解决
将网状结构分离为星型结构
关键代码
将相关对象的通信封装到一个类中单独处理
class Mediator;
class Person //抽象同事类
{
public:
virtual ~Person(){}
virtual void setMediator(Mediator* mediator)
{
m_mediator = mediator;
}
virtual void sendMessage(const string& message) = 0;
virtual void getMessage(const string& message) = 0;
protected:
Mediator* m_mediator;
};
class Mediator //抽象中介类
{
public:
virtual ~Mediator(){}
virtual void setBuyer(Person* buyer) = 0;
virtual void setSeller(Person* seller) = 0;
virtual void send(const string& message, Person* person) = 0;
};
class Buyer:public Person //买家类
{
public:
void sendMessage(const string& message)
{
m_mediator->send(message, this);
}
void getMessage(const string& message)
{
cout << "Buyer Get: " << message.data() << endl;
}
};
class Seller:public Person //卖家类
{
public:
void sendMessage(const string& message)
{
m_mediator->send(message, this);
}
void getMessage(const string& message)
{
cout << "Seller Get: " << message.data() << endl;
}
};
class HouseMediator:public Mediator //具体的中介类
{
public:
HouseMediator():m_buyer(nullptr),m_seller(nullptr){}
void setBuyer(Person* buyer)
{
m_buyer = buyer;
}
void setSeller(Person *seller)
{
m_seller = seller;
}
void send(const string& message, Person* person)
{
if(person == m_buyer)
{
m_seller->getMessage(message);
}
if(person == m_seller)
{
m_buyer->getMessage(message);
}
}
private:
Person* m_buyer;
Person* m_seller;
};
int main()
{
Person* buyer = new Buyer;
Person* seller = new Seller;
Mediator* houseMediator = new HouseMediator;
buyer->setMediator(houseMediator);
seller->setMediator(houseMediator);
houseMediator->setBuyer(buyer);
houseMediator->setSeller(seller);
buyer->sendMessage("1.5?");
seller->sendMessage("2!!!");
return 0;
}
参考:
https://www.yuque.com/docs/share/4009dbce-eaec-4f28-bd23-b19858231b78