设计模式之中介者模式(Mediator )
中介者模式是关于数据交互的设计模式,该模式的核心是一个中介者对象,负责协调一系列对象之间的不同的数据请求,这一系列对象成为同事类。如房产中介(简直不想提它),买房的卖房的,租房的放租的都到房产中介那里去登记。如果有卖房的就会通知买房的去买房,如果有放租的就会通知租房的去租房。所有的事物都是通过中介进行通知转换,这样就形成了一个典型的星型结构,说道星型结构,网络中的交换机路由器不就是个大大的中介者么。
作用
中介者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显作用。从而使它们可以松散耦合。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用。保证这些作用可以彼此独立的变化。
类视图

实现
中介者模式为拍卖行角色,负责买家、卖家和货币转换的协调工作,其中印度买家和法国买家想拍卖美国卖家的东西,印度使用的是卢比,法国使用的是美元,而美国使用的是美元,所有的出价都要以美元进行结算;这样就需要拍卖行来协调进行价格换算,拍卖竞价等工作。
//头文件mediator.h
#include <string>
class  Mediator;
class Colleage //同事类
{
public:
    Colleage(Mediator* md);
protected:
    Mediator *m_mediator;
};
class IndianBuyer: public Colleage
{
public:
    IndianBuyer(Mediator* md);
    void setTotalMoney(float fall);
    int Purchase(float bid);
private:
    float m_total_money; //买家的心理价位
};
class FrenchBuyer: public Colleage
{
public:
    FrenchBuyer(Mediator* md);
    void setTotalMoney(float fall);
    int Purchase(float bid);
private:
    float m_total_money;
};
class AmericanSeller: public Colleage
{
public:
    AmericanSeller(Mediator* md);
    void SetWantPrice(float price);
    bool IsBidAccept(float bidInDollars);
private:
    float m_priceInDollars; //卖家的心理价位
};
class DollorConver: public Colleage
{
public:
    DollorConver(Mediator* md);
    float ConverToDollars(float bid, std::string strName);
private:
    float ConverRupeetoDollar(float bid);
    float ConverEurotoDollar(float bid);
private:
    float dollor_unit; //美元换算比例
    float euro_unit;  //欧元换算比例
    float rupee_unit;//卢比换算比例
};
class Mediator
{
public:
    Mediator();
    void RegisterIndianBuyer(IndianBuyer* buyer);
    void RegisterFrenchBuyer(FrenchBuyer* buyer);
    void RegisterAmericanSeller(AmericanSeller* seller);
    void RegisterDollorConver(DollorConver* conver);
    bool placeBid(float bid,std::string strName);
private:
    IndianBuyer* m_pIndian;
    FrenchBuyer* m_pFrench;
    AmericanSeller* m_pAmerican;
    DollorConver* m_pConver;
};
mediator.cpp
#include <iostream>
#include "mediator.h"
using namespace std;
Colleage::Colleage(Mediator* md):m_mediator(md)
{
}
IndianBuyer::IndianBuyer(Mediator* md):Colleage(md),m_total_money(-1){
    m_mediator->RegisterIndianBuyer(this);
}
void IndianBuyer::setTotalMoney(float fall)
{
    m_total_money = fall;
}
int IndianBuyer::Purchase(float bid)
{
    //价格合适就出价一次
    if (m_total_money<0 || bid<= m_total_money)
    {
       return (int)m_mediator->placeBid(bid,"RUPEE");
    }
    else
    {
        return 2;//价格太高了  不要啦
    }
}
FrenchBuyer::FrenchBuyer(Mediator* md):Colleage(md),m_total_money(-1){
    m_mediator->RegisterFrenchBuyer(this);
}
void FrenchBuyer::setTotalMoney(float fall)
{
    m_total_money = fall;
}
int FrenchBuyer::Purchase(float bid)
{
    if (m_total_money<0 || bid<= m_total_money)
    {
       return (int)m_mediator->placeBid(bid,"EURO");
    }
    else
    {
        return 2;
    }
}
AmericanSeller::AmericanSeller(Mediator* md):Colleage(md),m_priceInDollars(0){
    m_mediator->RegisterAmericanSeller(this);
}
void AmericanSeller::SetWantPrice(float price)
{
    m_priceInDollars = price;
}
bool AmericanSeller::IsBidAccept(float bidInDollars)
{
    if (bidInDollars>=m_priceInDollars)
    {
       //当遇到价格增长时记录最高的价格,没有人超过这个价格就按照这个价格出售
        m_priceInDollars = bidInDollars;
        return true;
    }
    return false;
}
DollorConver::DollorConver(Mediator* md):Colleage(md)
,dollor_unit(1.0),euro_unit(0.7),rupee_unit(45.0){
    m_mediator->RegisterDollorConver(this);
}
float DollorConver::ConverToDollars(float bid, std::string strName)
{
    if (strName.compare("RUPEE")==0)
    {
        return ConverRupeetoDollar(bid);
    }
    else
    {
        return ConverEurotoDollar(bid);
    }
}
float DollorConver::ConverRupeetoDollar(float bid)
{
    return bid*(dollor_unit/rupee_unit);
}
float DollorConver::ConverEurotoDollar(float bid)
{
    return bid*(dollor_unit/euro_unit);
}
Mediator::Mediator():m_pIndian(NULL),m_pFrench(NULL)
,m_pAmerican(NULL),m_pConver(NULL){
}
void Mediator::RegisterIndianBuyer(IndianBuyer* buyer)
{
    m_pIndian = buyer;
}
void Mediator::RegisterFrenchBuyer(FrenchBuyer* buyer)
{
    m_pFrench = buyer;
}
void Mediator::RegisterAmericanSeller(AmericanSeller* seller)
{
    m_pAmerican = seller;
}
void Mediator::RegisterDollorConver(DollorConver* conver)
{
    m_pConver = conver;
}
bool Mediator::placeBid(float bid,std::string strName)
{
    float dollars =  m_pConver->ConverToDollars(bid,strName);
    return m_pAmerican->IsBidAccept(dollars);
}
int main(int argc,char *argv[])
{
    Mediator mediatior;
    IndianBuyer indian(&mediatior);
    FrenchBuyer french(&mediatior);
    AmericanSeller american(&mediatior);
    DollorConver conver(&mediatior);
    indian.setTotalMoney(6000);
    french.setTotalMoney(100);
    american.SetWantPrice(50);
    int  nIndian = 0;
    int  nFrench = 0;
    float IndianBid = 2000;
    float FrenchBid = 30;
    //一轮一轮进行出价,当有一个出不起的时候,就结束竞价。
    while(nIndian+nFrench<=2)
    {
        do{
            nIndian = indian.Purchase(IndianBid);
            IndianBid+=100;
            if (nIndian == 1)
            {
                cout<<"indian purchase : "<< IndianBid <<endl;
            }
        }
        while(nIndian==0);
        do{
            nFrench = french.Purchase(FrenchBid);
            FrenchBid+=1.5;
            if (nFrench == 1)
            {
                cout<<"french purchase : "<< FrenchBid <<endl;
            }
        }
        while(nFrench==0);
    }
    return 0;
}
当我们开始任何产品研发的时候总会有一些类,这些类会使用到之前产品的研发成果,随着功能的增加,逻辑会变得更加复杂,我们会添加更多的类和之前的类互相作用,知道难以维护所有的代码。中介者模式关心的就是这个问题,它会使代码更容易维护。它能够实现类之间的松散耦合。只有中介者这一个类知道所有的类,其他类只需要与中介者进行交互即可,当然更加集中的控制也会带来中枢的庞大,还是需要避免过度的集成。
应用场景
- 一组对象使用了标准的通信方式,但整体通信的连接都非常复杂,由此产生的相互依赖的结构导致系统难以结构化,也很难理解;
- 由于对象之间的通信和相互引用,导致对象难以重用。
- 分布于对个类中间的行为能够统一定制化,而无需创建过多的子类。
设计模式之中介者模式(Mediator )的更多相关文章
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
		原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ... 
- 二十四种设计模式:中介者模式(Mediator Pattern)
		中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ... 
- [设计模式] 17 中介者模式 Mediator Pattern
		在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ... 
- 设计模式之中介者模式(Mediator)摘录
		23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程.它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ... 
- 大熊君说说JS与设计模式之------中介者模式Mediator
		一,总体概要 1,笔者浅谈 我们从日常的生活中打个简单的比方,我们去房屋中介租房,房屋中介人在租房者和房东出租者之间形成一条中介.租房者并不关心他租谁的房.房东出租者也不关心他租给谁.因为有中介的存在 ... 
- 设计模式 笔记 中介者模式 Mediator
		//---------------------------15/04/27---------------------------- //Mediator 中介者模式----对象行为型模式 /* 1:意 ... 
- 行为型设计模式之中介者模式(Mediator)
		结构 意图 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 适用性 一组对象以定义良好但是复杂的方式进行通信.产生的相互 ... 
- 【设计模式】—— 中介者模式Mediator
		前言:[模式总览]——————————by xingoo 模式意图 使用一个中介的对象,封装一组对象之间的交互,这样这些对象就可以不用彼此耦合. 这个中介者常常起着中间桥梁的作用,使其他的对象可以利用 ... 
- 设计模式之中介者模式(Mediator)
		中间者模者模式原理:中介者维持所有要交互对象的指针或者对象,所有对象维持一个中介者的指针或者对象. #include <iostream> #include <string> ... 
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
		设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ... 
随机推荐
- Couchbase忘记登录密码怎么办
			以下都为root用户操作: 1.先关闭couchbase /opt/couchbase/etc/couchbase_init.d stop 2.切换到下面的路径,删除这个目录下除logs的所有文件,按 ... 
- 创建以mybatis为基础的web项目(2)mabitis中的一对一关系项目实战
			mabitis中的一对一关系项目实战: 1.首先根据创建以mybatis为基础的web项目(1)中的流程将web项目部署好 打开IDE,建立web工程 在lib下面粘贴mybatis的jar,log4 ... 
- JS字符串和数组常用方法
			1.indexOf() – 返回字符串中一个字符第一处出现的索引,接收2个参数:要查找的字符,从哪个位置开始查找:.lastIndexOf()--返回字符串中某一个字符最后一次出现的索引值. 如果没有 ... 
- ACE入门——ACE构建
			ACE(ADAPTIVE Communication Environment),ACE入门的第一课就是要学习怎么在自己的系统上构建ACE. ACE是跨平台的,这是它的一个很重要的特性,ACE支持很多的 ... 
- 爱奇艺2018春招Java工程师编程题题解
			字典序最大子序列 题目描述 对于字符串a和b,如果移除字符串a中的一些字母(可以全部移除,也可以一个都不移除)就能够得到字符串b我们就称b是a的子序列. 例如."heo"是&quo ... 
- [NOIp 2013]货车运输
			Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重 ... 
- no zuo no die
			#include <iostream> #include <cstring> #include <cstdio> using namespace std; name ... 
- NOIP 2008 双栈排序
			题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ... 
- AtCoder Grand Contest 002 D - Stamp Rally
			Description We have an undirected graph with N vertices and M edges. The vertices are numbered 1 thr ... 
- hdu 5700区间交(线段树)
			区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ... 
