设计模式之Mediator(中介者)

Mediator中介者模式定义:
用一个中介对象来封装一系列关于对象交互行为.

为何使用Mediator?
各个对象之间的交互操作非常多;每个对象的行为操作都依赖彼此对方,修改一个对象的行为,同时会涉及到修改很多其他对象的行为,如果使用Mediator模式,可以使各个对象间的耦合松散,只需关心和 Mediator的关系,使多对多的关系变成了一对多的关系,可以降低系统的复杂性,提高可修改扩展性.

如何使用?

首先 有一个接口,用来定义成员对象之间的交互联系方式:

public interface Mediator { }

Meiator具体实现,真正实现交互操作的内容:

public class ConcreteMediator implements Mediator {

   //假设当前有两个成员.
   private ConcreteColleague1 colleague1 = new ConcreteColleague1(); 
   private ConcreteColleague2 colleague2 = new ConcreteColleague2();

   ...

}

再看看另外一个参与者:成员,因为是交互行为,都需要双方提供一些共同接口,这种要求在Visitor Observer等模式中都是相同的.

public class Colleague {
   private Mediator mediator;
   public Mediator getMediator() { 
      return mediator;
   }

   public void setMediator( Mediator mediator ) { 
      this.mediator = mediator; 
   }
}

public class ConcreteColleague1 { }

public class ConcreteColleague2 { }

每个成员都必须知道Mediator,并且和 Mediator联系,而不是和其他成员联系.

至此,Mediator模式框架完成,可以发现Mediator模式规定不是很多,大体框架也比较简单,但实际使用起来就非常灵活.

Mediator模式在事件驱动类应用中比较多,例如界面设计GUI.;聊天,消息传递等,在聊天应用中,需要有一个MessageMediator,专门负责request/reponse之间任务的调节.

MVC是J2EE的一个基本模式,View Controller是一种Mediator,它是Jsp和服务器上应用程序间的Mediator.

 

===============================================================================

设计模式Mediator的C++实现源码

1、抽象类Media定义

#ifndef MEDIATOR_H
#define MEDIATOR_H
#pragma warning(disable:4786)

#include <map>
#include <string>
#include "Colleague.h"
using namespace std;

class Colleague;
typedef pair<string, Colleague*> Init_Pair;

class Mediator
{
public:
 virtual void AddColleague(Colleague*);
 virtual void Notified(Colleague*) = 0;
 
protected:
 map<string,Colleague*> m_map;
};

#endif

 

2、抽象类Mediator实现

#include "Mediator.h"

void Mediator::AddColleague(Colleague* c)
{
 m_map.insert(Init_Pair(c->GetName(),c));
}

 

3、抽象类Colleague定义

#ifndef COLLEAGUE_H
#define COLLEAGUE_H

#include "Mediator.h"

class Mediator;

class Colleague
{
public:
 Colleague(Mediator*);
 ~Colleague();
 virtual void Changed();
 virtual void Action() = 0;
 virtual char* GetName() = 0;

private:
 Mediator* m_pm;
};

#endif

 

4、抽象类Colleague实现

#include "Colleague.h"

Colleague::Colleague(Mediator* media) : m_pm(media)
{

}

Colleague::~Colleague()
{

}

void Colleague::Changed()
{
 if(m_pm != NULL)
 {
  m_pm->Notified(this);
 }
}

 

5、具体类ConcreteMediator定义

#ifndef CONCRETEMEDIATOR_H
#define CONCRETEMEDIATOR_H

#include "Mediator.h"

class ConcreteMediator : public Mediator
{
public:
 ConcreteMediator();
 ~ConcreteMediator();
 void Notified(Colleague*);
};

#endif

 

6、具体类ConcreteColleague实现

#include "ConcreteMediator.h"

#define COLLEAGUE1 "ConcreteColleague1"
#define COLLEAGUE2 "ConcreteColleague2"
#define COLLEAGUE3 "ConcreteColleague3"

ConcreteMediator::ConcreteMediator()
{

}

ConcreteMediator::~ConcreteMediator()
{

}

void ConcreteMediator::Notified(Colleague* c)
{
 map<string,Colleague*>::const_iterator iter;
 if(!strcmp(c->GetName(),COLLEAGUE1))
 {
  printf("ConcreteColleague1 changed!/n");
  iter = m_map.find(COLLEAGUE2);
  iter->second->Action();
  iter = m_map.find(COLLEAGUE3);
  iter->second->Action();
 }
 else if(!strcmp(c->GetName(),COLLEAGUE2))
 {
  printf("ConcreteColleague2 changed!/n");
  iter = m_map.find(COLLEAGUE1);
  iter->second->Action();
  iter = m_map.find(COLLEAGUE3);
  iter->second->Action();
 }
 else if(!strcmp(c->GetName(),COLLEAGUE3))
 {
  printf("ConcreteColleague3 changed!/n");
  iter = m_map.find(COLLEAGUE1);
  iter->second->Action();
  iter = m_map.find(COLLEAGUE2);
  iter->second->Action();
 }
}

 

7、具体类ConcreteColleague1定义

#ifndef CONCRETECOLLEAGUE1_H
#define CONCRETECOLLEAGUE1_H

#include "Colleague.h"

class ConcreteColleague1 : public Colleague
{
public:
 ConcreteColleague1(Mediator*);
 ~ConcreteColleague1();
 char* GetName();
 void Action();
};

#endif

 

8、具体类ConcreteColleague1实现

#include "ConcreteColleague1.h"

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

}

ConcreteColleague1::~ConcreteColleague1()
{

}

char* ConcreteColleague1::GetName()
{
 return "ConcreteColleague1";
}

void ConcreteColleague1::Action()
{
 printf("ConcreteColleague1::Action()/n");
}

 

9、具体类ConcreteColleague2实现

#ifndef CONCRETECOLLEAGUE2_H
#define CONCRETECOLLEAGUE2_H

#include "Colleague.h"

class ConcreteColleague2 : public Colleague
{
public:
 ConcreteColleague2(Mediator*);
 ~ConcreteColleague2();
 char* GetName();
 void Action();
};

#endif

 

10、具体类ConcreteColleague2实现

#include "ConcreteColleague2.h"

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

}

ConcreteColleague2::~ConcreteColleague2()
{

}

char* ConcreteColleague2::GetName()
{
 return "ConcreteColleague2";
}

void ConcreteColleague2::Action()
{
 printf("ConcreteColleague2::Action()/n");
}

 

11、具体实现类ConcreteColleague3定义

#ifndef CONCRETECOLLEAGUE3_H
#define CONCRETECOLLEAGUE3_H

#include "Colleague.h"

class ConcreteColleague3 : public Colleague
{
public:
 ConcreteColleague3(Mediator*);
 ~ConcreteColleague3();
 char* GetName();
 void Action();
};

#endif

 

12、具体实现类ConcreteColleague3实现

#include "ConcreteColleague3.h"

ConcreteColleague3::ConcreteColleague3(Mediator* media) : Colleague(media)
{

}

ConcreteColleague3::~ConcreteColleague3()
{

}

char* ConcreteColleague3::GetName()
{
 return "ConcreteColleague3";
}

void ConcreteColleague3::Action()
{
 printf("ConcreteColleague3::Action()/n");
}

 

13、客户使用类Client实现

#include "Colleague.h"
#include "Mediator.h"
#include "ConcreteMediator.h"
#include "ConcreteColleague1.h"
#include "ConcreteColleague2.h"
#include "ConcreteColleague3.h"

int main()
{
 Mediator* md = new ConcreteMediator();
 Colleague* c1 = new ConcreteColleague1(md);
 Colleague* c2 = new ConcreteColleague2(md);
 Colleague* c3 = new ConcreteColleague3(md);
 md->AddColleague(c1);
 md->AddColleague(c2);
 md->AddColleague(c3);
 c1->Changed();
 c2->Changed();
 c3->Changed();

 delete c3;
 c3 =NULL;
 delete c2;
 c2 = NULL;
 delete c1;
 c1 = NULL;
 delete md;
 md = NULL;

 return 0;
}


 

发布了519 篇原创文章 · 获赞 97 · 访问量 182万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览