设计模式-Mediator(行为模式) 用于将对象间的交互和通讯封装在一个类中。

以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码

//Mediator.h

#pragma once
class Colleage;
class Mediator
{
public:
    virtual ~Mediator();
    virtual void DoActionFromAtoB() = 0;
    virtual void DoActionFromBtoA() = 0;
protected:
    Mediator();
private:
};

class ConcreateMediator:public Mediator
{
public:
    ConcreateMediator();
    ConcreateMediator(Colleage* clgA, Colleage* clgB);
    virtual ~ConcreateMediator();
    void SetConcreateColleageA(Colleage* clgA);
    void SetConcreateColleageB(Colleage* clgB);
    Colleage* GetConcreateColleageA();
    Colleage* GetConcreateColleageB();
    void IntroColleage(Colleage* clgA, Colleage* clgB);
    void DoActionFromAtoB();
    void DoActionFromBtoA();
protected:
private:
    Colleage* _clgA;
    Colleage* _clgB;
};

//Mediator.cpp

#include"Colleage.h"
#include"Mediator.h"
Mediator::Mediator(){}
Mediator::~Mediator(){}
ConcreateMediator::ConcreateMediator(){}
ConcreateMediator::~ConcreateMediator(){}
ConcreateMediator::ConcreateMediator(Colleage* clgA, Colleage* clgB)
{
    this->_clgA = clgA;
    this->_clgB = clgB;
}

void ConcreateMediator::DoActionFromAtoB()
{
    _clgB->SetState(_clgA->GetState());
}

void ConcreateMediator::DoActionFromBtoA()
{
    _clgA->SetState(_clgB->GetState());
}

void ConcreateMediator::SetConcreateColleageA(Colleage* clgA)
{
    this->_clgA = clgA;
}
void ConcreateMediator::SetConcreateColleageB(Colleage* clgB)
{
    this->_clgB = clgB;
}

Colleage* ConcreateMediator::GetConcreateColleageA()
{
    return _clgA;
}

Colleage* ConcreateMediator::GetConcreateColleageB()
{
    return _clgB;
}

void ConcreateMediator::IntroColleage(Colleage*clgA, Colleage*clgB)
{
    this->_clgA = clgA;
    this->_clgB = clgB;
}

//Colleage.h

#pragma once
#include<string>
class Mediator;
class Colleage
{
public:
    virtual ~Colleage() = 0;
    virtual void Action() = 0;
    virtual void SetState(const std::string&sdt) = 0;
    virtual std::string GetState() = 0;
protected:
    Colleage();
    Colleage(Mediator*mdt);
    Mediator* _mdt;
private:

};

class ConcreateColleageA :public Colleage
{
public:
    ConcreateColleageA();
    ~ConcreateColleageA();
    ConcreateColleageA(Mediator* mdt);
    void Action();
    void SetState(const std::string&sdt);
    std::string GetState();
protected:
private:
    std::string _sdt;
};

class ConcreateColleageB :public Colleage
{
public:
    ConcreateColleageB();
    ~ConcreateColleageB();
    ConcreateColleageB(Mediator* mdt);
    void Action();
    void SetState(const std::string&sdt);
    std::string GetState();
protected:
private:
    std::string _sdt;
};

//Colleage.cpp

#include"Colleage.h"
#include"Mediator.h"
#include<iostream>

Colleage::Colleage(){}
Colleage::Colleage(Mediator* mdt){
    this->_mdt = mdt;
}
Colleage::~Colleage(){}
ConcreateColleageA::ConcreateColleageA()
{

}
ConcreateColleageA::~ConcreateColleageA()
{

}
ConcreateColleageA::ConcreateColleageA(Mediator* mdt) :Colleage(mdt)
{

}
std::string ConcreateColleageA::GetState()
{
    return _sdt;
}

void ConcreateColleageA::SetState(const std::string& sdt)
{
    this->_sdt = sdt;
}

void ConcreateColleageA::Action()
{
    _mdt->DoActionFromAtoB();
    std::cout << "State of ConcreateColleageB:" << this->GetState() << std::endl;
}

ConcreateColleageB::ConcreateColleageB()
{}

ConcreateColleageB::~ConcreateColleageB()
{}

ConcreateColleageB::ConcreateColleageB(Mediator*mdt) :Colleage(mdt)
{}

std::string ConcreateColleageB::GetState()
{
    return _sdt;
}
void ConcreateColleageB::SetState(const std::string& sdt)
{
    this->_sdt = sdt;
}

void ConcreateColleageB::Action()
{
    _mdt->DoActionFromBtoA();
    std::cout << "State of ConcreateColleageA:" << this->GetState() << std::endl;
}

//main.cpp

#include"Colleage.h"
#include"Mediator.h"
#include<iostream>

int main(int args, char* argv)
{
    ConcreateMediator* m = new ConcreateMediator();
    ConcreateColleageA* c0 = new ConcreateColleageA(m);
    ConcreateColleageB* c1 = new ConcreateColleageB(m);
    m->IntroColleage(c0, c1);
    c0->SetState("Old");
    c1->SetState("Old");
    c0->Action();
    c1->Action();
    std::cout << std::endl;
    c0->SetState("New");
    c0->Action();
    c1->Action();
    std::cout << std::endl; 
    c1->SetState("Old");
    c1->Action();
    c0->Action();
    getchar();
    return 0;
}
posted @ 2019-12-23 16:52  fourmii  阅读(249)  评论(0编辑  收藏  举报