行为型模式-中介者(mediator)

中介者

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

实例

main.cc:

#include "seller_people.h"
#include "buyer_people.h"
#include "hourse_mediator.h"
#include 

/*
design_pattern:"mediator"
2016 most of the fire is not the real estate.
Buyers and sellers should be through an intermediary to facilitate transactions, 
buyers and sellers to sign the contract is do not even know each other, 
only know intermediary information both the buyer and the seller 
does not need to know each other through the intermediary of communication.
*/
int main(){
    HourseMediator *hourse_mediator = new HourseMediator();
    SellerPeole *seller = new SellerPeole(hourse_mediator);
    BuyerPeole *buyer = new BuyerPeole(hourse_mediator);
    hourse_mediator->SetBuyer(buyer);
    hourse_mediator->SetSeller(seller);
    seller->Say("There is room for sale, 50000¥ per square meter");
    buyer->Say("I want to buy a house, the total price of 3000000");

    //clear
    delete hourse_mediator;
    delete seller;
    delete buyer;
    system("Pause");
    return 0;
}

Mediator:

//mediator.h
#ifndef HELENDP_SOURCE_MEDIATOR_H_
#define HELENDP_SOURCE_MEDIATOR_H_
#include 
using namespace std;
#include "people.h"

class Mediator{
public:
    Mediator();
    ~Mediator();
    virtual void Communicate(People *people,string message) = 0;
};
#endif


//mediator.cc
#include "mediator.h"

Mediator::Mediator(){

}

Mediator::~Mediator(){

}

HourseMediator:

//hourse_mediator.h
#ifndef HELENDP_SOURCE_HOURSE_MEDIATOR_H_
#define HELENDP_SOURCE_HOURSE_MEDIATOR_H_
#include "mediator.h"
#include "seller_people.h"
#include "buyer_people.h"

class HourseMediator : public Mediator{
public:
    HourseMediator();
    ~HourseMediator();
    void SetBuyer(BuyerPeole *buyer);
    void SetSeller(SellerPeole *seller);
    void Communicate(People *people,string message);
private:
    SellerPeole *seller_people_;
    BuyerPeole  *buyer_people_;
};
#endif


//hourse_mediator.cc
#include "hourse_mediator.h"
#include 
using namespace std;

HourseMediator::HourseMediator(){
}

HourseMediator::~HourseMediator(){

}

void HourseMediator::SetBuyer(BuyerPeole * buyer){
    buyer_people_ = buyer;
}

void HourseMediator::SetSeller(SellerPeole * seller){
    seller_people_ = seller;
}

void HourseMediator::Communicate(People *people,string message){
    if(people == seller_people_){
        buyer_people_->ReceiveMessage(message);
    }
    else if(people == buyer_people_){
        seller_people_->ReceiveMessage(message);
    }
    else
        cout << "error!" << endl;
}

People:

//people.h
#ifndef HELENDP_SOURCE_PEOPLE_H_
#define HELENDP_SOURCE_PEOPLE_H_
#include 
using namespace std;
class Mediator;

class People{
public:
    People(Mediator *mediator);
    ~People();
public:
    Mediator *mediator_;
};
#endif


//people.cc
#include "people.h"
#include "mediator.h"
People::People(Mediator *mediator){
    mediator_ = mediator;
}

People::~People(){

}

SellerPeople:

//seller_people.h
#ifndef HELENDP_SOURCE_SELLER_PEOPLE_H_
#define HELENDP_SOURCE_SELLER_PEOPLE_H_
#include "people.h"

class SellerPeole : public People{
public:
    SellerPeole(Mediator* mediator_);
    ~SellerPeole();

    void Say(string message);
    void ReceiveMessage(string message);
};
#endif


//seller_people.cc
#include "seller_people.h"
#include "mediator.h"
#include 
using namespace std;

SellerPeole::SellerPeole(Mediator* mediator) : People(mediator){

}

SellerPeole::~SellerPeole(){

}

void SellerPeole::Say(string message){
    mediator_->Communicate(this,message);
}

void SellerPeole::ReceiveMessage(string message){
    cout << "Seller receive message:" << message << endl;
}

BuyerPeople:

//buyer_people.h
#ifndef HELENDP_SOURCE_BUYER_PEOPLE_H_
#define HELENDP_SOURCE_BUYER_PEOPLE_H_
#include "people.h"

class BuyerPeole : public People{
public:
    BuyerPeole(Mediator* mediator_);
    ~BuyerPeole();

    void Say(string message);
    void ReceiveMessage(string message);
};
#endif


//buyer_people.cc
#include "buyer_people.h"
#include "mediator.h"
#include 
using namespace std;

BuyerPeole::BuyerPeole(Mediator* mediator) : People(mediator){

}

BuyerPeole::~BuyerPeole(){

}

void BuyerPeole::Say(string message){
    mediator_->Communicate(this,message);
}

void BuyerPeole::ReceiveMessage(string message){
    cout << "buyer receive message:" << message << endl;
}

代码和UML图(EA)工程文件,最后会整理打包上传.

UML类图

行为型模式-中介者(mediator)_第1张图片

结构

  • Mediator(Mediator):中介者接口类.
  • ConcreteMediator(HourseMediator):具体中介者类.
  • Colleague(People):抽象同事类.
  • ConcreteColleague(SellerPeople,BuyerPeople):具体同事类.

优点

  • 简化了对象之间的交互.
  • 将各同事解耦.
  • 减少子类生成.
  • 可以简化各同事类的设计和实现.

缺点

  • 在具体中介者类中包含了同事之间的交互细节,可能会导致具体中介者类非常复杂,使得系统难以维护.

你可能感兴趣的:(设计模式)