1.Mediator Pattern

  • Mediator模式将对象间的交互和通讯封装在一个类中,各个对象间的通信不必显示去声明和引用,将多对多的通信转化为一对多的通信,大大降低了系统的复杂性能。
  • 通过Mediator,各个Colleage就不必维护各自通信的对象和通信协议,降低了系统的耦合性。
  • Mediator模式还有一个很显著额特点就是将控制集中,集中的优点就是便于管理,也正符合了OO设计中的每个类的职责要单一和集中的原则

2.Mediator模式结构图

Mediator Pattern

3.实现

 1 #ifndef _COLLEAGE_H_ 
 2 #define _COLLEAGE_H_
 3 
 4 #include <string> 
 5 using namespace std;
 6 class Mediator;
 7 
 8 class Colleage 
 9 { 
10 public:
11     virtual ~Colleage();
12     virtual void Aciton() = 0;
13     virtual void SetState(const string& sdt) = 0;
14     virtual string GetState() = 0; 
15 protected:
16     Colleage();
17     Colleage(Mediator* mdt);
18     Mediator* _mdt;
19 private:
20 };
21 
22 class ConcreteColleageA:public Colleage 
23 { 
24 public:
25     ConcreteColleageA();
26     ConcreteColleageA(Mediator* mdt);
27     ~ConcreteColleageA();
28     void Aciton();
29     void SetState(const string& sdt);
30     string GetState();
31 protected:
32 private: 
33     string _sdt;
34 };
35 
36 class ConcreteColleageB:public Colleage 
37 { 
38 public: 
39     ConcreteColleageB();
40     ConcreteColleageB(Mediator* mdt);
41     ~ConcreteColleageB();
42     void Aciton();
43     void SetState(const string& sdt);
44     string GetState();
45 protected:
46 private: 
47     string _sdt;
48 };
49 
50 #endif
Colleage.h

相关文章:

  • 2021-08-08
  • 2021-11-22
  • 2021-10-19
  • 2021-06-11
  • 2021-08-29
猜你喜欢
  • 2021-12-04
  • 2021-06-12
  • 2021-11-10
  • 2021-11-22
  • 2021-12-30
  • 2022-01-14
  • 2021-05-30
相关资源
相似解决方案