I. Example
For example, China and Japan have disputes over the Diaoyu Islands. At this time, the United Nations will come up and serve as a mediator. In fact, there is no good mediation. The Diaoyu Islands are China's, and this is an indisputable fact! The United Nations is also a speaker and spokesman.
The structure is as follows:
The Code is as follows:
[Cpp] // abstract country
Class Country
{
Protected:
Mediator * m_mediator; // intermediary
Public:
Virtual void SetMediator (Mediator * mediator) {} // sets the mediation
Virtual void SendMessage (string message) {}// sends a message to the intermediary
Virtual void GetMessage (string message) {}// obtain information from the intermediary
};
// Abstract intermediary
Class Mediator
{
Public:
Virtual void Send (string message, Country * person ){}
Virtual void SetJanpa (Country * Janpa) {}// let the intermediary know the specific object
Virtual void SetChina (Country * China ){}
};
// Japan
Class Janpa: public Country
{
Public:
Void SetMediator (Mediator * mediator) {m_mediator = mediator ;}
Void SendMessage (string message) {m_mediator-> Send (message, this );}
Void GetMessage (string message) {cout <"Japanese message received:" <message ;}
};
// China
Class China: public Country
{
Public:
Void SetMediator (Mediator * mediator) {m_mediator = mediator ;}
Void SendMessage (string message) {m_mediator-> Send (message, this );}
Void GetMessage (string message) {cout <"China received message:" <message ;}
};
// United Nations
Class UN: public Mediator
{
Private:
Country * m_Janpa; // Japan
Country * m_China; // China
Public:
UN (): m_Janpa (NULL), m_China (NULL ){}
Void SetJanpa (Country * Janpa) {m_Janpa = Janpa ;}
Void SetChina (Country * China) {m_China = China ;}
Void Send (string message, Country * country)
{
If (country = m_Janpa) // send a message to the image library in Japan
M_China-> GetMessage (message); // China receives the message
Else
M_Janpa-> GetMessage (message );
}
};
// Test code
Int main ()
{
Mediator * mediator = new UN ();
Country * pJanpa = new Janpa (); // Japan
Country * pChina = new China (); // China
Mediator-> SetJanpa (Janpa );
Mediator-> SetChina (China );
PJanpa-> SetMediator (mediator );
PChina-> SetMediator (mediator );
PJanpa-> SendMessage ("Diaoyu Islands is ours, hahaha... \ n ");
PChina-> SendMessage ("fuck, you just need to fart again. Be careful when you're killed! \ N ");
Delete pJanpa;
Delete pChina;
Delete mediator;
Return 0;
}
// Abstract country
Class Country
{
Protected:
Mediator * m_mediator; // intermediary
Public:
Virtual void SetMediator (Mediator * mediator) {} // sets the mediation
Virtual void SendMessage (string message) {}// sends a message to the intermediary
Virtual void GetMessage (string message) {}// obtain information from the intermediary
};
// Abstract intermediary
Class Mediator
{
Public:
Virtual void Send (string message, Country * person ){}
Virtual void SetJanpa (Country * Janpa) {}// let the intermediary know the specific object
Virtual void SetChina (Country * China ){}
};
// Japan
Class Janpa: public Country
{
Public:
Void SetMediator (Mediator * mediator) {m_mediator = mediator ;}
Void SendMessage (string message) {m_mediator-> Send (message, this );}
Void GetMessage (string message) {cout <"Japanese message received:" <message ;}
};
// China
Class China: public Country
{
Public:
Void SetMediator (Mediator * mediator) {m_mediator = mediator ;}
Void SendMessage (string message) {m_mediator-> Send (message, this );}
Void GetMessage (string message) {cout <"China received message:" <message ;}
};
// United Nations
Class UN: public Mediator
{
Private:
Country * m_Janpa; // Japan
Country * m_China; // China
Public:
UN (): m_Janpa (NULL), m_China (NULL ){}
Void SetJanpa (Country * Janpa) {m_Janpa = Janpa ;}
Void SetChina (Country * China) {m_China = China ;}
Void Send (string message, Country * country)
{
If (country = m_Janpa) // send a message to the image library in Japan
M_China-> GetMessage (message); // China receives the message
Else
M_Janpa-> GetMessage (message );
}
};
// Test code
Int main ()
{
Mediator * mediator = new UN ();
Country * pJanpa = new Janpa (); // Japan
Country * pChina = new China (); // China
Mediator-> SetJanpa (Janpa );
Mediator-> SetChina (China );
PJanpa-> SetMediator (mediator );
PChina-> SetMediator (mediator );
PJanpa-> SendMessage ("Diaoyu Islands is ours, hahaha... \ n ");
PChina-> SendMessage ("fuck, you just need to fart again. Be careful when you're killed! \ N ");
Delete pJanpa;
Delete pChina;
Delete mediator;
Return 0;
}
Ii. intermediary Mode
Definition: a mediation object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.
Note:
1. In the Mediator mode, each Colleague maintains a Mediator. When communication is required, each specific Colleague sends a message directly to ConcreteMediator. The ConcreteMediator determines where the message is sent.
2. ConcreteColleagueA and ConcreteColleagueB do not have to maintain their own references, or even they do not know the existence of each.
3. The advantage is that various colleags reduce coupling.
4. The disadvantage is that because Mediator controls centralization, the interaction complexity between colleags is changed to the complexity of the intermediary, that is, the intermediary is more complex than any Colleague.
Author lwbeyond