前言

中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

@startuml
class Colleage {
+ virtual void action() = 0;
+ virtual void setState(const string &state) = 0;
+ virtual string getState() = 0;
# Colleage(mediator *mediator);
# mediator *_mediator{};
}

class ConcreteColleageA {
# ConcreteColleageA(mediator *mediator);
# void action();
# void setState(const string &state);
# string getState();
- string state;
}


class ConcreteColleageB {
+ ConcreteColleageB(mediator *mediator);
+ void action();
+ void setState(const string &state);
+ string getState();
- string state;
}

class mediator {
+ virtual void doActionFromAtoB() = 0;
+ virtual void doActionFromBtoA() = 0;
# mediator() = default;
}

class ConcreteMediator
{
+ ConcreteMediator(Colleage* colleageA, Colleage* colleageB);
+ void setConcreteColleageA(Colleage* colleageA);
+ void setConcreteColleageB(Colleage* colleageB);
+ Colleage* getConcreteColleageA();
+ Colleage* getConcreteColleageB();
+ void introColleage(Colleage* colleageA, Colleage* colleageB);
+ void doActionFromAtoB();
+ void doActionFromBtoA();
- Colleage* colleageA;
- Colleage* colleageB;
}

Colleage <|-- ConcreteColleageA
Colleage <|-- ConcreteColleageB
mediator <|-- ConcreteMediator

ConcreteMediator -> ConcreteColleageA
ConcreteMediator -> ConcreteColleageB
mediator <- Colleage

@enduml

代码

colleage.h

#ifndef COLLEAGE_H
#define COLLEAGE_H

#include <string>

using namespace std;

class mediator;

class Colleage {
public:
    virtual ~Colleage() = default;
    virtual void action() = 0;
    virtual void setState(const string &state) = 0;
    virtual string getState() = 0;
protected:
    Colleage() = default;
    Colleage(mediator *mediator);
    mediator *_mediator{};
};

class ConcreteColleageA : public Colleage {
public:
    ConcreteColleageA() = default;
    ConcreteColleageA(mediator *mediator);
    ~ConcreteColleageA() = default;
    void action();
    void setState(const string &state);
    string getState();
private:
    string state;
};

class ConcreteColleageB : public Colleage {
public:
    ConcreteColleageB() = default;
    ConcreteColleageB(mediator *mediator);
    ~ConcreteColleageB() = default;
    void action();
    void setState(const string &state);
    string getState();
private:
    string state;
};

#endif //COLLEAGE_H

colleage.cpp

#include "colleage.h"
#include "mediator.h"
#include <iostream>
using namespace std;

Colleage::Colleage(mediator *mediator)
{
    this->_mediator = mediator;
}

ConcreteColleageA::ConcreteColleageA(mediator *mediator): Colleage(mediator){}

string ConcreteColleageA::getState() {return state;}

void ConcreteColleageA::setState(const std::string &state) {this->state = state;}

void ConcreteColleageA::action() {
    _mediator->doActionFromAtoB();
    cout<<"State of ConcreteColleageB "<< this->getState()<<endl;
}

ConcreteColleageB::ConcreteColleageB(mediator* mediator): Colleage(mediator) {}

void ConcreteColleageB::action() {
    _mediator->doActionFromBtoA();
    cout<<"State of ConcreteColleageB "<<this->getState()<<endl;
}

string ConcreteColleageB::getState() {return state;}

void ConcreteColleageB::setState(const std::string &state) {this->state = state;}

mediator.h

#ifndef MEDIATOR_H
#define MEDIATOR_H
class Colleage;

class mediator {
public:
    virtual ~mediator() = default;
    virtual void doActionFromAtoB() = 0;
    virtual void doActionFromBtoA() = 0;
protected:
    mediator() = default;
};

class ConcreteMediator:public mediator
{
public:
    ConcreteMediator() = default;
    ConcreteMediator(Colleage* colleageA, Colleage* colleageB);
    ~ConcreteMediator() = default;
    void setConcreteColleageA(Colleage* colleageA);
    void setConcreteColleageB(Colleage* colleageB);
    Colleage* getConcreteColleageA();
    Colleage* getConcreteColleageB();
    void introColleage(Colleage* colleageA, Colleage* colleageB);
    void doActionFromAtoB();
    void doActionFromBtoA();
private:
    Colleage* colleageA;
    Colleage* colleageB;
};

#endif //MEDIATOR_H

mediator.cpp

#include "mediator.h"
#include "colleage.h"

ConcreteMediator::ConcreteMediator(Colleage *colleageA, Colleage *colleageB) {
    this->colleageA = colleageA;
    this->colleageB = colleageB;
}

void ConcreteMediator::doActionFromAtoB() {
    this->colleageB->setState(colleageA->getState());
}

void ConcreteMediator::setConcreteColleageA(Colleage *colleageA) {
    this->colleageA = colleageA;
}

void ConcreteMediator::setConcreteColleageB(Colleage *colleageB) {
    this->colleageB = colleageB;
}
Colleage* ConcreteMediator::getConcreteColleageA() {
    return colleageA;
}

Colleage* ConcreteMediator::getConcreteColleageB() {
    return colleageB;
}
void ConcreteMediator::introColleage(Colleage *colleageA, Colleage *colleageB) {
    this->colleageA = colleageA;
    this->colleageB = colleageB;
}
void ConcreteMediator::doActionFromBtoA() {
    colleageA->setState(colleageB->getState());
}

main.cpp

#include "mediator.h"
#include "colleage.h"
#include <iostream>

using namespace std;

int main() {
    ConcreteMediator* mediator = new ConcreteMediator();
    ConcreteColleageA* concrete1 = new ConcreteColleageA(mediator);
    ConcreteColleageB* concrete2 = new ConcreteColleageB(mediator);
    mediator->introColleage(concrete1, concrete2);

    concrete1->setState("old");
    concrete2->setState("old");
    concrete1->action();
    concrete2->action();
    cout<<endl;

    concrete1->setState("new");
    concrete1->action();
    concrete2->action();
    cout<<endl;

    concrete2->setState("old");
    concrete2->action();
    concrete1->action();

    return 0;
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部