大话设计模式--中介者模式 Mediator --C++实现实例

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;
}







 

 

 

 

 

 

 

posted @ 2013-10-17 10:59  今晚打酱油_  阅读(263)  评论(0编辑  收藏  举报