mediator模式

作者: 老练子丶2017 | 来源:发表于2017-11-24 16:04 被阅读0次

将对象封装在类似中介的类中,对象之间的通信他们本身不用关心

mediator.h

#ifndef _MEDIATOR_H

#define _MEDIATOR_H

#include <iostream>

#include <string>

using namespace std;

class Mediator

{

public:

Mediator() {

}

~Mediator() {

}

virtual void DoActionFromAtoB()=0;

virtual void DoActionFromBtoA()=0;

};

class Colleage

{

public:

virtual ~Colleage() {

}

virtual void Action()=0;

Colleage() {

}

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

}

public:

Mediator* _mdt;

string _sdt;

};

class ConcreteColleageA : public Colleage

{

public:

ConcreteColleageA() {

}

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

}

~ConcreteColleageA() {

}

void Action() {

_mdt->DoActionFromAtoB();

cout << "state of concreteColleageA:" << _sdt << endl;

}

public:

string _sdt;

};

class ConcreteColleageB : public Colleage

{

public:

ConcreteColleageB() {

}

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

}

~ConcreteColleageB() {

}

void Action() {

_mdt->DoActionFromBtoA();

cout << "state of concreteColleageB:" << _sdt << endl;

}

public:

string _sdt;

};

class ConcreteMediator : public Mediator

{

public:

ConcreteMediator() {

}

~ConcreteMediator() {

}

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

}

void DoActionFromAtoB() {

_clgB->_sdt = _clgA->_sdt;

}

void DoActionFromBtoA() {

_clgA->_sdt = _clgB->_sdt;

}

void IntroColleage(Colleage* clgA, Colleage* clgB) {

_clgA = clgA;

_clgB = clgB;

}

public:

Colleage* _clgA;

Colleage* _clgB;

};

#endif // _MEDIATOR_H

mediator.cpp

#include "mediator.h"

int main()

{

ConcreteMediator* m = new ConcreteMediator;

ConcreteColleageA* c1 = new ConcreteColleageA(m);

ConcreteColleageB* c2 = new ConcreteColleageB(m);

m->IntroColleage(c1, c2);

c1->_sdt = "old";

c2->_sdt = "old";

c1->Action();

c2->Action();

c1->_sdt = "new";

c1->Action();

c2->Action();

c2->_sdt = "old";

c2->Action();

c1->Action();

return 0;

}

编译: make mediator

相关文章

  • 中介者模式

    介绍 中介者模式 (Mediator Pattern) 也称为调解者模式或调停者模式,Mediator 本身就有调...

  • astron设计模式学习手记之调停者模式

    调停者模式 图解设计模式第16章 mediator别名:mediator,中介者模式说白了,就是同事之间互相沟通成...

  • mediator模式

    将对象封装在类似中介的类中,对象之间的通信他们本身不用关心 mediator.h #ifndef _MEDIATO...

  • Mediator模式

    组员向仲裁者报告,仲裁者向组员下达指示,组员之间不在相互询问和相互指示。 书中这样描述仲裁者模式,这种模式也比较好...

  • 组件化的几个方案对比

    组件化(一) CTMediator 设计模式:中介(Mediator)模式 + Target-Action模式 ...

  • 中介者模式(Mediator)

    中介者模式(Mediator) 中介者模式(Mediator)就是用一个中介对象来封装一系列的对象交互,中介者使各...

  • iOS 模块化开发调研

    casa 的方案:Mediator模式 + target-action模式,target就是class,actio...

  • 设计模式[21]-中介者模式-Mediator Pattern

    1.中介者模式简介 中介者模式(Mediator Pattern)模式是是行为型(Behavioral)设计模式,...

  • 设计模式-iOS常见

    一、单例模式 系统的单例模式(Singleton Pattern) 二、中介者模式 中介者模式(Mediator ...

  • iOS 设计模式-中介者模式

    1.中介者模式简介 中介者模式(Mediator Pattern) 也称为调解者模式或调停者模式,Mediat...

网友评论

    本文标题:mediator模式

    本文链接:https://www.haomeiwen.com/subject/byrfbxtx.html