简体   繁体   中英

Mediator Design pattern as a test system

Here we have my implementation of the mediator design pattern 调解人

The Goal is to create a working/useful instance of the Mediator pattern, the code works fine. The question is this an appropriate use of the pattern, and was it executed correctly?

I using mediator very often but in c++ in qt. However I thing this is does not matter. So I can something tell you.

I think that your example is good. Generaly connection as look like

Object ---> Mediator ----> Object

Mediator should inherit form Abstrac Mediator. Abstrac mediator provide interface. Mediator also should have vector or list all object (pointer) who whant to send and get data beetwen as. I whant to show my header file from my mediator.

class Object;
class GlobalMediator;
class Mediator;

class IMediator
{
public: 
    virtual void Send(QString from , QString to, QString message) = 0;
    virtual void Send(QString from , QString to, QString parametr , QString message) = 0;
    virtual void Send(QString from , QString to , QGraphicsItem *item) = 0;
    virtual void Send(QString from , QString to , QString message , QVector<QString> vect) = 0;

//    virtual void SendFromOut(QString from , QString to, QString message) = 0;
};

class Mediator :  public QObject , public IMediator
{
    Q_OBJECT
private:
    QMap<QString ,Object* > map;
    GlobalMediator *gMed;
    QString name;

public:
    Mediator(QString name);
    QString getName();
    void Register(Object* obj);
    void Unregister(QString name );
    void setGlobalMediator(GlobalMediator *gm);

    void Send(QString from , QString to,  QString message);
    void Send(QString from , QString to, QString parametr , QString message);
    void Send(QString from , QString to , QGraphicsItem *item);
    void Send(QString from , QString to , QString message , QVector<QString> vect);



};

class GlobalMediator
{
private:
    QMap<QString , Mediator*> map;
public:
    GlobalMediator();
    void RegisterMediator(Mediator *med);
    void UnregisterMediator(Mediator *med);
\

};



class Object
{
//    Q_OBJECT


public:
    Object(Mediator *medium , QString name);
    Object(QString name);

private:
    Mediator *medium;

protected:
    QString name;

public: //others
    void AddMediator(Mediator *med);
    Mediator* getMediator();
    QString _name();

public:
    virtual void Send(QString to , QString message);
    virtual void Send(QString to, QString parametr , QString message);
    virtual void Send(QString to, QGraphicsItem *item);
    virtual void Send(QString to , QString message , QVector<QString> vect);

    virtual void Receive(QString from, QString message);
    virtual void Receive(QString from, QString parametr , QString message);
    virtual void Receive(QString from, QGraphicsItem *item);
    virtual void Receive(QString from , QString message , QVector<QString> vect);

};

#endif // MEDIATOR_H

As You see you also need create class Object. Object is sepcjal class provided interface for concret object each whant to join to mediator list. This patern works very well.

I also introduced GlobalMEdiator. This is some like network simply Mediator is some like LAN, and GlobalMediator is some like WAN. In your example GlobalMediator worked if Local mediator not found object. Then Local mediator send message to GlobalMediator and he send message to all LocalMediator each he have in its List. Let say A_Local_Mediator have all student and teacher in one class. B_Local_Mediator have all studdent and teacher in class B. When teacher form class A whant to send info to student from calss B mesage is routed. In this code GlobalMediator not have full implemented as you see. If you whant that I can provided this. In this moment I found the only same conception.

TeacherA ---> A_Local_Mediator (if find student send mesage to student in this same calss if not ---> Global_ Mediator---->B_Local_Mediator (find student)

For what this ??? In this case your have very simply mechanism to managment Mediator and Object. Generaly in small project GlobalMediator is not needed. So you can comment this few lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM