C++设计模式-中介者模式

目录

 

 

基本概念

代码与实例


 

基本概念

中介者模式(Mediator)用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示的相互作用,从而使其耦合松散,而且可以独立的改变他们之间的关系。

下面是UML图此图来源于大话设计模式

C++设计模式-中介者模式_第1张图片

 

代码与实例

程序运行截图如下:

C++设计模式-中介者模式_第2张图片

源码如下:

Head.h

#ifndef HEAD_H
#define HEAD_H

#include 
#include 
using namespace std;

class Colleague;
class ConcreteColleague1;
class ConcreteColleague2;

//Mediator类 抽象中介者
class Mediator{

public:
	virtual void send(const string &message, Colleague *colleague);
	virtual ~Mediator();

	Mediator();
	Mediator(Mediator &m);
	Mediator &operator = (Mediator &m);
};

// Colleague类 抽象同事类
class Colleague{

public:
	Colleague(Mediator *mediator);
	~Colleague();

protected:
	Mediator *m_mediator;
};

// ConcreteMediator类 具体中介者类
class ConcreteMediator: public Mediator{

public:
	void setConcreteColleague1(ConcreteColleague1 *colleague);
	void setConcreteColleague2(ConcreteColleague2 *colleague);
	void send(const string &message, Colleague *colleague);
	~ConcreteMediator();

private:
	ConcreteColleague1 *m_colleague1;
	ConcreteColleague2 *m_colleague2;
};

//ConcreteColleague1和ConcreteColleague2等各种同事对象
class ConcreteColleague1: public Colleague{

public:
	ConcreteColleague1(Mediator *mediator);
	~ConcreteColleague1();
	void send(const string &message);
	void notify(const string &message);

};

class ConcreteColleague2: public Colleague{

public:
	ConcreteColleague2(Mediator *mediator);
	~ConcreteColleague2();
	void send(const string &message);
	void notify(const string &message);

};

#endif

Head.cpp

#include "Head.h"
#include 


void Mediator::send(const string &message, Colleague *colleague)
{

}

Mediator::~Mediator()
{
	cout << "Mediator::~Mediator()" << endl;
}

Mediator::Mediator()
{

}

Mediator::Mediator(Mediator &m)
{

}

Mediator & Mediator::operator=(Mediator &m)
{
	return Mediator();
}

Colleague::Colleague(Mediator *mediator)
{
	m_mediator = mediator;
}

Colleague::~Colleague()
{
	cout << "Colleague::~Colleague()" << endl;
}

void ConcreteMediator::setConcreteColleague1(ConcreteColleague1 *colleague)
{
	m_colleague1 = colleague;
}

void ConcreteMediator::setConcreteColleague2(ConcreteColleague2 *colleague)
{
	m_colleague2 = colleague;
}

void ConcreteMediator::send(const string &message, Colleague *colleague)
{
	if(colleague == m_colleague1){

		m_colleague1->notify(message);
	}
	else if(colleague == m_colleague2){

		m_colleague2->notify(message);
	}
	else{

		assert(!"雅喵蝶");
	}
}

ConcreteMediator::~ConcreteMediator()
{
	cout << "ConcreteMediator::~ConcreteMediator()" << endl;
}

ConcreteColleague1::ConcreteColleague1(Mediator *mediator): Colleague(mediator)
{

}

ConcreteColleague1::~ConcreteColleague1()
{
	cout << "ConcreteColleague1::~ConcreteColleague1()" << endl;
}

void ConcreteColleague1::send(const string &message)
{
	m_mediator->send(message, this);
}

void ConcreteColleague1::notify(const string &message)
{
	cout << "同事1得到信息: " << message << endl;
}

ConcreteColleague2::ConcreteColleague2(Mediator *mediator): Colleague(mediator)
{

}

ConcreteColleague2::~ConcreteColleague2()
{
	cout << "ConcreteColleague2::~ConcreteColleague2()" << endl;
}

void ConcreteColleague2::send(const string &message)
{
	m_mediator->send(message, this);
}

void ConcreteColleague2::notify(const string &message)
{
	cout << "同事2得到信息: " << message << endl;
}

main.cpp

#include "Head.h"

int main(int *argc, int *argv[]){

	ConcreteMediator *m = new ConcreteMediator;
	ConcreteColleague1 *c1 = new ConcreteColleague1(m);
	ConcreteColleague2 *c2 = new ConcreteColleague2(m);

	m->setConcreteColleague1(c1);
	m->setConcreteColleague2(c2);

	c1->send("吃饭了吗?");
	c2->send("你猜!");

	cout << endl << endl << "---------华丽的分割线---------" << endl << endl;

	delete m;
	delete c1;
	delete c2;

	getchar();
	return 0;
}

这个例子的中介模式,就是通过2个同事通过中介类去对话,这里每一个ConcreteColleague都认识ConcreteMediator,并且每一个ConcreteMediator认识所有的ConcreteColleague。

其中一个很有特点的代码就是这个:

void ConcreteColleague1::send(const string &message)
{
	m_mediator->send(message, this);
}

随后ConcreteMediator在根据传过来的this,进行传话。

你可能感兴趣的:(C/C++,设计模式,C,C++,设计模式)