C++中介者模式(Mediator)

 C++
时间:

应用场景

1、系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象。

2、想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。

Mediator

  • Colleage.h
#ifndef __COLLEAGE_H__
#define __COLLEAGE_H__

#include<string>
#include<iostream>
using namespace std;

class Mediator;

class Colleage
{
    public:
        virtual ~Colleage();
        virtual void Aciton() = 0;
        virtual void SetSate(const string& sdt) = 0;
        virtual string GetState() = 0;
    protected:
        Colleage();
        Colleage(Mediator* mdt);
        Mediator* _mdt;
    private:
};

class ConcreteColleageA : public Colleage
{
    public:
        ConcreteColleageA();
        ConcreteColleageA(Mediator* mdt);
        ~ConcreteColleageA();
        void Aciton();
        void SetSate(const string& sdt);
        string GetState();
    protected:
    private:
        string _sdt;
};

class ConcreteColleageB : public Colleage
{
    public:
        ConcreteColleageB();
        ConcreteColleageB(Mediator* mdt);
        ~ConcreteColleageB();
        void Aciton();
        void SetSate(const string& sdt);
        string GetState();
    protected:
    private:
        string _sdt;
};

#endif
  • Colleage.cpp
#include"Colleage.h"
#include"Mediator.h"

Colleage::Colleage()
{

}

Colleage::~Colleage()
{

}

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


ConcreteColleageA::ConcreteColleageA()
{

}

ConcreteColleageA::ConcreteColleageA(Mediator* mdt) : Colleage(mdt)
{

}

ConcreteColleageA::~ConcreteColleageA()
{

}

void ConcreteColleageA::Aciton()
{
    _mdt->DoActionFromAtoB();
    cout << "State of ConcreteColleageB:" << this->GetState() << endl;
}

void ConcreteColleageA::SetSate(const string& sdt)
{
    _sdt = sdt;
}

string ConcreteColleageA::GetState()
{
    return _sdt;
}


ConcreteColleageB::ConcreteColleageB()
{

}

ConcreteColleageB::ConcreteColleageB(Mediator* mdt) : Colleage(mdt)
{

}

ConcreteColleageB::~ConcreteColleageB()
{

}

void ConcreteColleageB::Aciton()
{
    _mdt->DoActionFromAtoA();
    cout << "State of ConcreteColleageA:" << this->GetState() << endl;
}

void ConcreteColleageB::SetSate(const string& sdt)
{
    _sdt = sdt;
}

string ConcreteColleageB::GetState()
{
    return _sdt;
}
  • Mediator.h
#ifndef __MEDIATOR_H__
#define __MEDIATOR_H__

class Colleage;

class Mediator
{
    public:
        ~Mediator() {}
        virtual void DoActionFromAtoB() = 0;
        virtual void DoActionFromAtoA() = 0;
    protected:
        Mediator() {}
    private:
};

class ConcreteMediator : public Mediator
{
    public:
        ConcreteMediator();
        ConcreteMediator(Colleage* clgA, Colleage* clgB);
        ~ConcreteMediator();
        void SetConcreteColleageA(Colleage* clgA);
        void SetConcreteColleageB(Colleage* clgB);
        Colleage* GetConcreteColleageA();
        Colleage* GetConcreteColleageB();
        void IntroColleage(Colleage* clgA, Colleage* clgB);
        void DoActionFromAtoB();
        void DoActionFromAtoA();
    protected:
    private:
        Colleage* _clgA;
        Colleage* _clgB;
};

#endif
  • Mediator.cpp
#include"Mediator.h"
#include"Colleage.h"


ConcreteMediator::ConcreteMediator()
{

}

ConcreteMediator::ConcreteMediator(Colleage* clgA, Colleage* clgB)
{
    _clgA = clgA;
    _clgB = clgB;
}

ConcreteMediator::~ConcreteMediator()
{

}

void ConcreteMediator::SetConcreteColleageA(Colleage* clgA)
{
    _clgA = clgA;
}

void ConcreteMediator::SetConcreteColleageB(Colleage* clgB)
{
    _clgB = clgB;
}

Colleage* ConcreteMediator::GetConcreteColleageA()
{
    return _clgA;
}

Colleage* ConcreteMediator::GetConcreteColleageB()
{
    return _clgB;
}

void ConcreteMediator::IntroColleage(Colleage* clgA, Colleage* clgB)
{
    _clgA = clgA;
    _clgB = clgB;
}

void ConcreteMediator::DoActionFromAtoB()
{
    _clgB->SetSate(_clgA->GetState());
}

void ConcreteMediator::DoActionFromAtoA()
{
    _clgA->SetSate(_clgB->GetState());
}
  • main.cpp
#include<iostream>
#include"Mediator.h"
#include"Colleage.h"
using namespace std;

int main(int argc, char* argv[])
{

    ConcreteMediator* m = new ConcreteMediator();

    ConcreteColleageA* c1 = new ConcreteColleageA(m);
    ConcreteColleageB* c2 = new ConcreteColleageB(m);

    m->IntroColleage(c1, c2);

    c1->SetSate("old");
    c2->SetSate("old");
    c1->Aciton();
    c2->Aciton();
    cout << endl;

    c1->SetSate("new");
    c1->Aciton();
    c2->Aciton();
    cout << endl;

    c2->SetSate("old");
    c2->Aciton();
    c1->Aciton();
    cout << endl;
    return 0;
}

0 评论