zoukankan      html  css  js  c++  java
  • Mediator Pattern

    1.Mediator Pattern

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

    2.Mediator模式结构图

    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
     1 #include "Mediator.h"
     2 #include "Colleage.h"
     3 #include <iostream> 
     4 using namespace std;
     5 
     6 Colleage::Colleage() 
     7 { 
     8     //_sdt = " "; 
     9 }
    10 Colleage::Colleage(Mediator* mdt) 
    11 { 
    12     this->_mdt = mdt;
    13     //_sdt = " "; 
    14 }
    15 Colleage::~Colleage() 
    16 {
    17     
    18 }
    19 ConcreteColleageA::ConcreteColleageA()
    20 {
    21 
    22 }
    23 ConcreteColleageA::~ConcreteColleageA() 
    24 {
    25 
    26 }
    27 ConcreteColleageA::ConcreteColleageA(Mediator* mdt):Colleage(mdt) 
    28 {
    29 
    30 }
    31 string ConcreteColleageA::GetState() 
    32 { 
    33     return _sdt;
    34 }
    35 void ConcreteColleageA::SetState(const string& sdt) 
    36 { 
    37     _sdt = sdt;
    38 }
    39 void ConcreteColleageA::Aciton() 
    40 { 
    41     _mdt->DoActionFromAtoB();
    42     cout<<"State of ConcreteColleageB:"<<" "<<this->GetState()<<endl; 
    43 }
    44 ConcreteColleageB::ConcreteColleageB()
    45 {
    46 
    47 }
    48 ConcreteColleageB::~ConcreteColleageB() 
    49 {
    50 
    51 }
    52 ConcreteColleageB::ConcreteColleageB(Mediator* mdt):Colleage(mdt)
    53 {
    54 
    55 }
    56 void ConcreteColleageB::Aciton() 
    57 { 
    58     _mdt->DoActionFromBtoA();
    59     cout<<"State of ConcreteColleageB:"<<" "<<this->GetState()<<endl; 
    60 }
    61 string ConcreteColleageB::GetState() 
    62 { 
    63     return _sdt; 
    64 }
    65 void ConcreteColleageB::SetState(const string& sdt) 
    66 { 
    67     _sdt = sdt; 
    68 }
    Colleage.cpp
     1 #ifndef _MEDIATOR_H_ 
     2 #define _MEDIATOR_H_
     3 
     4 class Colleage;
     5 
     6 class Mediator 
     7 { 
     8 public: 
     9     virtual ~Mediator();
    10     virtual void DoActionFromAtoB() = 0;
    11     virtual void DoActionFromBtoA() = 0;
    12 protected: 
    13     Mediator();
    14 private:
    15 };
    16 
    17 class ConcreteMediator:public Mediator
    18 { 
    19 public:
    20     ConcreteMediator();
    21     ConcreteMediator(Colleage* clgA,Colleage* clgB);
    22     ~ConcreteMediator();
    23     void SetConcreteColleageA(Colleage* clgA);
    24     void SetConcreteColleageB(Colleage* clgB);
    25     Colleage* GetConcreteColleageA();
    26     Colleage* GetConcreteColleageB();
    27     void IntroColleage(Colleage* clgA,Colleage* clgB);
    28     void DoActionFromAtoB();
    29     void DoActionFromBtoA();
    30 
    31 protected:
    32 private: 
    33     Colleage* _clgA;
    34     Colleage* _clgB;
    35 };
    36 
    37 #endif
    Mediator.h
     1 #include "Mediator.h"
     2 #include "Colleage.h"
     3 
     4 Mediator::Mediator() 
     5 {
     6 
     7 }
     8 Mediator::~Mediator()
     9 {
    10 
    11 }
    12 ConcreteMediator::ConcreteMediator()
    13 {
    14 
    15 }
    16 ConcreteMediator::~ConcreteMediator() 
    17 {
    18 
    19 }
    20 ConcreteMediator::ConcreteMediator(Colleage* clgA,Colleage* clgB) 
    21 { 
    22     this->_clgA = clgA;
    23     this->_clgB = clgB;
    24 }
    25 void ConcreteMediator::DoActionFromAtoB() 
    26 { 
    27     _clgB->SetState(_clgA->GetState());
    28 }
    29 void ConcreteMediator::SetConcreteColleageA(Colleage* clgA) 
    30 { 
    31     this->_clgA = clgA; 
    32 }
    33 void ConcreteMediator::SetConcreteColleageB(Colleage* clgB) 
    34 { 
    35     this->_clgB = clgB;
    36 }
    37 Colleage* ConcreteMediator::GetConcreteColleageA()
    38 {
    39     return _clgA; 
    40 }
    41 Colleage* ConcreteMediator::GetConcreteColleageB()
    42 { 
    43     return _clgB;
    44 }
    45 void ConcreteMediator::IntroColleage(Colleage* clgA,Colleage* clgB) 
    46 { 
    47     this->_clgA = clgA;
    48     this->_clgB = clgB; 
    49 }
    50 void ConcreteMediator::DoActionFromBtoA() 
    51 { 
    52     _clgA->SetState(_clgB->GetState()); 
    53 }
    Mediator.cpp
     1 #include "Mediator.h"
     2 #include "Colleage.h"
     3 
     4 #include <iostream> 
     5 using namespace std;
     6 
     7 int main(int argc,char* argv[]) 
     8 { 
     9     ConcreteMediator* m = new ConcreteMediator();
    10 
    11     ConcreteColleageA* c1 = new ConcreteColleageA(m); 
    12     ConcreteColleageB* c2 = new ConcreteColleageB(m);
    13 
    14     m->IntroColleage(c1,c2);
    15 
    16     c1->SetState("old"); 
    17     c2->SetState("old"); 
    18     c1->Aciton(); 
    19     c2->Aciton();
    20     cout<<endl;
    21 
    22     c1->SetState("new"); 
    23     c1->Aciton(); 
    24     c2->Aciton(); 
    25     cout<<endl;
    26 
    27     c2->SetState("old"); 
    28     c2->Aciton(); 
    29     c1->Aciton();
    30 
    31     return 0;
    32 }
    main.cpp
  • 相关阅读:
    前端面试题目汇总摘录(HTML 和 CSS篇)
    The Road to learn React书籍学习笔记(第三章)
    The Road to learn React书籍学习笔记(第二章)
    算法复习:动态规划
    算法复习:回溯法
    算法复习:图
    算法复习:二叉树专题
    算法复习:最短路Dijkstra
    算法复习:BFS与DFS
    算法复习:标记数组 / 数组
  • 原文地址:https://www.cnblogs.com/programmer-wfq/p/4670708.html
Copyright © 2011-2022 走看看