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. zabbix自动化监控三种方式

    1.agent自动注册2.sever端自动发现discovery3.zabbix api

  2. CentOS 源码安装svn

    一. 下载依赖包 1. apr源码包 http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.gz 2. apr-util源码包 h ...

  3. MySQL视图概述

    1.介绍 在传统关系型数据库里,视图有时也被称作虚表,是基于特定SQL结果集的虚拟数据表.在有些场合会变得很方便,例如:原有系统重构,旧的数据表A和B已经被拆分和合并到数据表C.D.F里面,为了实现平 ...

  4. 搜索maven的库中某个支持库的的最新版本

    首先放网址(建议挂个vpn): maven库中心:http://search.maven.org/ jcenter库中心:https://bintray.com/bintray/jcenter 接下来 ...

  5. openresty 定时器

    [1]nginx定时器应用 (1)文件目录结构 (2)nginx.conf配置 lua_package_path "/usr/local/lib/ubcsrvd/lualib/?.lua;; ...

  6. Android中经常使用的工具类02

    1.读取手机联系人信息 一般用在读取手机通讯录上传,这一块比較多. import java.util.ArrayList; import java.util.List; import android. ...

  7. WPF中的数据模板使用方式之一:ContentControl、ContentTemplate和TemplateSelector的使用

    在WPF中,数据模板是非常强大的工具,他是一块定义如何显示绑定的对象的XAML标记.有两种类型的控件支持数据模板:(1)内容控件通过ContentTemplate属性支持数据模板:(2)列表控件通过I ...

  8. RGBA与半透明背景

    概念 所谓RGBA颜色,就是RGB三原色加ALPHA.在给背景加入颜色的同一时候.提供透明度特性. 用法 background:rgba(90,90, 54, 0.5); 支持情况 Firefox 3 ...

  9. hdu3293(pell方程+快速幂)

    裸的pell方程. 然后加个快速幂. No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: ...

  10. excel生成随机数

    这个功能可以通过excel来实现,操作步骤如下:       1.新建一个excel,并打开       2.选中一个单元格,在单元格中填写:    =20*RAND()+30  确定之后就会发现已经 ...