Design mode_ 21 intermediary model

21 intermediary model

21.1 concept

Define a mediation role to encapsulate the interaction between a series of objects, so that the coupling between the original objects is loose, and the interaction between them can be changed independently.
Multiple classes are interrelated, showing a complex network structure and over coupling. The mediator pattern is introduced to change the relationship between classes into a star structure. Any change in a class will only affect the class itself and the mediator.

21.2 structure

Abstract intermediary role: defines the interface of the intermediary and provides abstract methods for registering and forwarding colleague object information.
Specific mediator role: implement the mediator interface, define a list to manage colleague objects, and coordinate the interaction between various colleague roles.
Abstract colleague class role: defines the interface of colleague class, saves the mediator object, provides the abstract method of colleague object interaction, and realizes the public functions of all interacting colleague classes.
Specific colleague roles: implement Abstract colleague roles. When you need to interact with other colleague objects, the mediator object is responsible for the subsequent interaction.

21.3 realization

21.3.1 UML diagram

21.3.2 code



using namespace std;

class Person;
class Tenant;
class HouseHolder;

class Mediator {
	virtual void constactTenant(string message, Tenant* tenant) = 0;
	virtual void constactHouseHolder(string message, HouseHolder* houseHolder) = 0;

class ConcreteMediator : public Mediator {
	list<Tenant*> tenants;
	list<HouseHolder*> houseHolders;
	void addHouseHolder(HouseHolder* houseHolder);
	void deleteHouseHolder(HouseHolder* houseHolder);
	void addTenant(Tenant* tenant);
	void deleteTenant(Tenant* tenant);
	void constactTenant(string message, Tenant * tenant);
	void constactHouseHolder(string message, HouseHolder* houseHolder);

class Person {
	string name;
	ConcreteMediator* mediator;
	Person(string name, ConcreteMediator* mediator);

class Tenant : public Person {
	Tenant(string name, ConcreteMediator* mediator);
	void constact(string message);
	void getMessage(string message);
	virtual ~Tenant();

class HouseHolder : public Person {
	HouseHolder(string name, ConcreteMediator* mediator);
	void constact(string message);
	void getMessage(string message);
	virtual ~HouseHolder();




//Intermediary class
void ConcreteMediator::addHouseHolder(HouseHolder* houseHolder) {
void ConcreteMediator::deleteHouseHolder(HouseHolder* houseHolder) {
void ConcreteMediator::addTenant(Tenant* tenant) {
void ConcreteMediator::deleteTenant(Tenant* tenant) {
void ConcreteMediator::constactTenant(string message, Tenant* tenant) {
	for (auto it : this->houseHolders) {
void ConcreteMediator::constactHouseHolder(string message, HouseHolder* houseHolder) {
	for (auto it : this->tenants) {

//Colleague class
Person::Person(string name, ConcreteMediator* mediator) {
	this->name = name;
	this->mediator = mediator;

//Tenant class
Tenant::Tenant(string name, ConcreteMediator* mediator) : Person(name, mediator) {
void Tenant::constact(string message) {
	this->mediator->constactTenant(this->name+"The requirements are:"+message, this);
void Tenant::getMessage(string message) {
	cout << this->name << "Message received:\n" << message << endl <<endl;
Tenant::~Tenant() {

HouseHolder::HouseHolder(string name, ConcreteMediator* mediator) : Person(name, mediator) {
void HouseHolder::constact(string message) {
	this->mediator->constactHouseHolder(this->name + "The requirements are:" + message, this);
void HouseHolder::getMessage(string message) {
	cout <<this->name <<"Message received:\n" << message << endl << endl;
HouseHolder::~HouseHolder() {



int main() {
	ConcreteMediator* mediator = new ConcreteMediator();
	Tenant* tenant1 = new Tenant("Renter 1", mediator);
	Tenant* tenant2 = new Tenant("Renter 2", mediator);
	HouseHolder* houseHolder1 = new HouseHolder("Homeowner 1", mediator);
	HouseHolder* houseHolder2 = new HouseHolder("Homeowner 2", mediator);
	tenant1->constact("We need a one bedroom house");
	houseHolder2->constact("There is a house with three bedrooms and two living rooms");
	return 0;

21.4 advantages and disadvantages

21.4.1 advantages

Loose coupling. Encapsulate the interaction between multiple colleague objects into the mediator object, so that the coupling between colleague objects is loose, so that colleague objects can be changed and reused independently.
Centralized control interaction. The interaction of multiple colleague objects is encapsulated in the mediator object for unified management, so that when the interaction behavior changes, only the mediation object needs to be modified.
One to many association is transformed into one to one association. Make the relationship easier to understand.

21.4.2 disadvantages

When there are too many colleagues, intermediaries will become very large and difficult to maintain.

21.5 usage scenarios

There are complex reference relationships in the system, and the system structure is chaotic and difficult to understand.
When you want to create an object running between multiple classes and don't want to generate new subclasses.

return overview of design patterns;

Back to design mode overview

Keywords: C++ Design Pattern

Added by summoner on Thu, 24 Feb 2022 17:00:14 +0200