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

Mediator的出现减少了各个Colleague的耦合,使得可以独立地改变和复用各个Colleague类和Mediator.

由于ConcreteMediator控制了集中化,于是就把交互复杂性变为了中介者的复杂性,这就使得中介者变为比任何一个ConcreteColleague都复杂。

中介者模式一般应用于一组对象以定义良好但复杂的方式进行通信的场合,以及想定制一个分部在多个类中的行为,而又不想生成太多的子类的场合。

实例:

colleague.h colleague.cpp

#ifndef COLLEAGUE_H
#define COLLEAGUE_H class Mediator; class Colleague
{
public:
Colleague(Mediator *mediator); protected:
Mediator *mediator;
}; #endif // COLLEAGUE_H
#include "colleague.h"
#include "mediator.h" Colleague::Colleague(Mediator *mediator)
{
this->mediator = mediator;
}

concretecolleague1.h concretecolleague1.cpp

#ifndef CONCRETECOLLEAGUE1_H
#define CONCRETECOLLEAGUE1_H #include "colleague.h"
#include "mediator.h"
#include <string>
using namespace std; class ConcreteColleague1 : public Colleague
{
public:
ConcreteColleague1(Mediator *mediator);
void send(string message);
void notify(string message);
}; #endif // CONCRETECOLLEAGUE1_H
#include "concretecolleague1.h"
#include <iostream>
using namespace std; ConcreteColleague1::ConcreteColleague1(Mediator *mediator) : Colleague(mediator)
{
} void ConcreteColleague1::send(string message)
{
mediator->send(message, this);
} void ConcreteColleague1::notify(string message)
{
cout << "Colleague1 get a message: " << message << endl;
}

concretecolleague2.h concretecolleague2.cpp

#ifndef CONCRETECOLLEAGUE2_H
#define CONCRETECOLLEAGUE2_H #include "colleague.h"
#include "mediator.h"
#include <string>
using namespace std; class ConcreteColleague2 : public Colleague
{
public:
ConcreteColleague2(Mediator *mediator);
void send(string message);
void notify(string message);
}; #endif // CONCRETECOLLEAGUE2_H
#include "concretecolleague2.h"
#include <iostream>
using namespace std; ConcreteColleague2::ConcreteColleague2(Mediator *mediator) : Colleague(mediator)
{
} void ConcreteColleague2::send(string message)
{
mediator->send(message, this);
} void ConcreteColleague2::notify(string message)
{
cout << "Colleague2 get a message: " << message << endl;
}

mediator.h mediator.cpp

#ifndef MEDIATOR_H
#define MEDIATOR_H #include "colleague.h"
#include <string>
using namespace std; class Mediator
{
public:
Mediator();
void virtual send(string message, Colleague *colleague)=0;
}; #endif // MEDIATOR_H
#include "mediator.h"

Mediator::Mediator()
{
}

concretemediator.h concretemediator.cpp

#ifndef CONCRETEMEDIATOR_H
#define CONCRETEMEDIATOR_H #include "mediator.h"
#include "concretecolleague1.h"
#include "concretecolleague2.h" class ConcreteMediator : public Mediator
{
public:
ConcreteMediator();
void setcolleague1(ConcreteColleague1 *colleague);
void setcolleague2(ConcreteColleague2 *colleague);
void send(string message, Colleague *colleague); private:
ConcreteColleague1 *colleague1;
ConcreteColleague2 *colleague2;
}; #endif // CONCRETEMEDIATOR_H
#include "concretemediator.h"

ConcreteMediator::ConcreteMediator()
{
} void ConcreteMediator::send(string message, Colleague *colleague)
{
if( colleague == colleague1 )
colleague2->notify(message);
else
colleague1->notify(message); } void ConcreteMediator::setcolleague1(ConcreteColleague1 *colleague)
{
this->colleague1 = colleague;
} void ConcreteMediator::setcolleague2(ConcreteColleague2 *colleague)
{
this->colleague2 = colleague;
}

main.cpp

#include <iostream>
#include "concretecolleague1.h"
#include "concretecolleague2.h"
#include "concretemediator.h"
using namespace std; int main()
{
cout << "Mediator test!" << endl; ConcreteMediator *m = new ConcreteMediator();
ConcreteColleague1 *c1 = new ConcreteColleague1(m);
ConcreteColleague2 *c2 = new ConcreteColleague2(m);
m->setcolleague1(c1);
m->setcolleague2(c2);
c1->send("I come from C1, how are you? ");
c2->send("I come from C2, I am fine. "); return 0;
}

大话设计模式--中介者模式 Mediator --C++实现实例的更多相关文章

  1. 设计模式-中介者模式(Mediator)

    场景分析: 众所周知,电脑有很多组成部分,如硬盘.内存.光驱.音频.键盘等,各个组件之间协同工作才能保证电脑的正常运行. 如果各个组件之间直接交互,可能会比较复杂,如下图: 将上面的各个组件抽象成类, ...

  2. 23种设计模式--中介者模式-Mediator Pattern

    一.中介者模式的介绍     中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...

  3. 大话设计模式--享元模式 Flyweight -- C++实现实例

    1. 享元模式: 运用共享技术有效地支持大量细粒度的对象. 享元模式可以避免大量非常相似类的开销,在程序设计中,有时需要生成大量颗粒度的类实例来表示数据,如果能发现这些实例除了几个参数外基本都是相同的 ...

  4. 大话设计模式--装饰者模式 Decorator -- C++实现实例

    1.装饰者模式 Decorator 动态地给一个对象添加一个额外的职责, 就添加功能来说, 装饰模式比生成子类更为灵活. 每个装饰对象的实现和如何使用这个对象分离,  每个装饰对象只关心自己的功能,不 ...

  5. 深入浅出设计模式——中介者模式(Mediator Pattern)

    模式动机 在用户与用户直接聊天的设计方案中,用户对象之间存在很强的关联性,将导致系统出现如下问题: 系统结构复杂:对象之间存在大量的相互关联和调用,若有一个对象发生变化,则需要跟踪和该对象关联的其他 ...

  6. 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...

  7. 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...

  8. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  9. 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

随机推荐

  1. php给图片添加文字水印方法汇总

    在php中要给图片加水印我们需要给php安装GD库了,这里我们不介绍GD库安装,只介绍怎么利用php给图片添加文字水印的4种方法的汇总.有需要的小伙伴可以参考下. 1: 面向过程的编写方法 1 2 3 ...

  2. Cannot open channel to 3 at election address :3888 java.net.ConnectException: Connection refused (Connection refused)

    关于Linux中搭建分布式时可能遇到的问题 这个问题来自于今天安装zookeeper时踩的一个大坑,害的我花了一天时间.在搭建zookeeper的分布式时,往往要进行这样的配置: server.1=h ...

  3. tigervnc环境搭建

    在root用户下执行以下操作: 1.安装tigervnc yum install tigervnc tigervnc-server 2.配置tigervnc 编辑vncservers文件,执行如下命令 ...

  4. maven 工程聚合插件

    <!-- war包生成插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <a ...

  5. java栈的最大深度?

    1. 概述 某公司面试,总监大叔过来,问了图论及栈的最大深度,然后^_^ 一直记着,今天搞一下 2. 代码 package com.goodfan.test; public class JavaSta ...

  6. 同样的代码在java和c++中结果不同

    #include <iostream> using namespace std; /* run this program using the console pauser or add y ...

  7. php闭包简单实例

    <?php function getClosure($i) { $i = $i.'-'.date('H:i:s'); return function ($param) use ($i) { ec ...

  8. C语言中的编译时分配内存

    1.栈区(stack) --编译器自动分配释放,主要存放函数的参数值,局部变量值等: 2.堆区(heap) --由程序员分配释放: 3.全局区或静态区 --存放全局变量和静态变量:程序结束时由系统释放 ...

  9. poj1408(求线段交点)

    求出所有线段的交点,然后利用叉乘求四边形面积即可. // // main.cpp // poj1408 // // Created by 陈加寿 on 15/12/31. // Copyright ( ...

  10. [Catalan数]1086 栈、3112 二叉树计数、3134 Circle

    1086 栈 2003年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 栈是计算机中 ...