• 设计模式-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;
    }
  • 相关阅读:
    easyui loadFilter 使用
    控件setText与setValue赋值顺序先后区别
    JS选中和取消选中checkbox
    easyui 解决连弹两个dialog时候,第二个dialog居中问题
    bootstrap基础学习【导航条、分页导航】(五)
    bootstrap基础学习【菜单、按钮、导航】(四)
    sublime设置
    《啊哈!算法》笔记
    《编码的奥秘》笔记
    Effective Objective-C 2.0 — 第14条:理解“类对象“的用意
  • 原文地址:https://www.cnblogs.com/fourmi/p/12084641.html
Copyright © 2020-2023  润新知