(三)行为模式:5、中介者模式(Mediator Pattern)(C++示例)

news/2024/7/6 2:55:42

目录

1、中介者模式(Mediator Pattern)含义

2、中介者模式的UML图学习

3、中介者模式的应用场景

4、中介者模式的优缺点

(1)优点

(2)缺点

5、C++实现中介者模式的实例


1、中介者模式(Mediator Pattern)含义

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

中介者模式是一种行为型设计模式,它通过引入一个中介者对象来协调多个相关对象之间的交互。中介者模式将对象之间的通信封装在中介者对象中,从而降低了对象之间的耦合性。

2、中介者模式的UML图学习

中介者模式的几个角色:

(1)中介者(Mediator):定义了各个相关对象之间通信的接口,通常包括发送消息、注册和移除对象等方法。

(2)具体中介者(Concrete Mediator):实现中介者接口,协调各个相关对象之间的通信。

(3)同事类(Colleague):定义了各个相关对象的接口,包括发送消息、接收消息等方法。

(4)具体同事类(Concrete Colleague):实现同事类接口,与其他同事类进行通信。

3、中介者模式的应用场景

(1)系统中对象之间存在复杂的交互关系,导致每个对象都需要与多个其他对象进行通信。

(2)对象之间的交互逻辑较为复杂,难以维护和理解。

(3)希望减少对象之间的直接依赖关系,提高系统的可扩展性和灵活性。

4、中介者模式的优缺点

(1)优点

        1)减少了对象之间的直接依赖:中介者模式将对象之间的通信逻辑封装在中介者对象中,使得对象之间不需要直接引用彼此,从而降低了对象之间的耦合性。

        2)简化了对象之间的交互:中介者模式集中了对象之间的交互逻辑,使得交互变得简单明确。

        3)提高了系统的可扩展性:由于对象之间的通信逻辑集中在中介者对象中,新增或修改一个相关对象不会影响其他对象。

(2)缺点

        1)中介者对象可能变得复杂:随着系统中对象之间交互关系的增加,中介者对象可能变得庞大复杂。

        2)违反了单一职责原则:中介者对象承担了协调各个对象之间通信的责任,可能导致其职责过重。

5、C++实现中介者模式的实例


#include <iostream>
#include <string>

class Colleague;

// 中介者接口
class Mediator 
{
public:
    virtual void sendMessage(const std::string& message, Colleague* colleague) = 0;
};

// 同事类
class Colleague 
{
protected:
    Mediator* mediator;

public:
    explicit Colleague(Mediator* med) : mediator(med) {}

    virtual void receiveMessage(const std::string& message) = 0;
    virtual void sendMessage(const std::string& message) = 0;
};

// 具体中介者
class ConcreteMediator : public Mediator 
{
private:
    Colleague* colleague1;
    Colleague* colleague2;

public:
    void setColleague1(Colleague* col) 
    {
        colleague1 = col;
    }

    void setColleague2(Colleague* col) 
    {
        colleague2 = col;
    }

    void sendMessage(const std::string& message, Colleague* colleague) override 
    {
        if (colleague == colleague1) 
        {
            colleague2->receiveMessage(message);
        }
         else if (colleague == colleague2) 
        {
            colleague1->receiveMessage(message);
        }
    }
};

// 具体同事类
class ConcreteColleague1 : public Colleague 
{
public:
    explicit ConcreteColleague1(Mediator* med) : Colleague(med) {}

    void receiveMessage(const std::string& message) override 
    {
        std::cout << "ConcreteColleague1 received: " << message << std::endl;
    }

    void sendMessage(const std::string& message) override 
    {
        mediator->sendMessage(message, this);
    }
};

class ConcreteColleague2 : public Colleague 
{
public:
    explicit ConcreteColleague2(Mediator* med) : Colleague(med) {}

    void receiveMessage(const std::string& message) override 
    {
        std::cout << "ConcreteColleague2 received: " << message << std::endl;
    }

    void sendMessage(const std::string& message) override 
    {
        mediator->sendMessage(message, this);
    }
};

int main() 
{
    ConcreteMediator mediator;
    ConcreteColleague1 colleague1(&mediator);
    ConcreteColleague2 colleague2(&mediator);

    mediator.setColleague1(&colleague1);
    mediator.setColleague2(&colleague2);

    colleague1.sendMessage("Hello, colleague2!");
    colleague2.sendMessage("Hi, colleague1!");

    return 0;
}

在上述示例中,我们定义了中介者接口Mediator和具体中介者ConcreteMediator,以及同事类Colleague和具体同事类ConcreteColleague。通过实现这些接口和类,我们可以创建一个中介者对象来协调两个同事对象之间的通信。


http://lihuaxi.xjx100.cn/news/1470030.html

相关文章

平板用的触控笔什么牌子好?开学必买电容笔清单

开学季马上就要到来了&#xff0c;自从iPad的Pencil火了之后&#xff0c;国内又陆续推出了一些新的产品&#xff0c;比如平替之类的&#xff0c;有的质量非常好&#xff0c;有的价格非常实惠&#xff0c;还能被大多数人所接受。不过&#xff0c;这里面有不少的产品&#xff0c;…

261 migo过账--工单发料 demo

261 migo过账–工单发料 demo goodsmvt_code 03.move_type 261.LOOP AT req_item.CALL FUNCTION CONVERSION_EXIT_ALPHA_INPUTEXPORTINGinput req_item-aufnrIMPORTINGoutput pro_noEXCEPTIONSOTHERS 1.goodsmvt_item-item_text tr_code.CLEAR tr_code.goodsmvt_item-ma…

js计算一串数字最长子链长度

假设有一串字符串"186 186 150 200 160 130 197 200"&#xff1b;现在求它的最长升序子串长度。 let str "186 186 150 200 160 130 197 200";let arr str.split(" "); // 转化为数组let arrLeft []; // 存储每个数左边小于其的数的个数for(l…

【Linux】网络层协议:IP

我们必须接受批评&#xff0c;因为它可以帮助我们走出自恋的幻象&#xff0c;不至于长久在道德和智识上自我陶醉&#xff0c;在自恋中走向毁灭&#xff0c;事实上我们远比自己想象的更伪善和幽暗。 文章目录 一、IP和TCP之间的关系&#xff08;提供策略 和 提供能力&#xff09…

DBIDRespOrd/DBIDResp的区别

DBIDResp: □ 用来通知requester, buffer资源已经可用了&#xff0c;可以接受writedata; □ 用来表示completer对这个请求已经保序处理&#xff1b; □ 在write, combined write, DVMOp, Atomic中使用&#xff1b; □ 用在如下节点之间&#xff1a; □ HN<->RN; □ S…

html写一个向flask_socketio发送消息和接收消息并显示在页面上

以下是一个简单的HTML页面&#xff0c;它包含一个输入框、一个发送按钮和一个显示区域。用户可以在输入框中输入消息&#xff0c;点击发送按钮&#xff0c;然后这个消息会被发送到 Flask-SocketIO 服务器。当服务器回应消息时&#xff0c;它会在页面的显示区域显示出来。 <…

随手笔记——雷达点云话题转PCL,处理数据后再转为雷达点云话题常用方式

随手笔记——雷达点云话题转PCL&#xff0c;处理数据后再转为雷达点云话题常用方式 说明主要函数 说明 将接收到的雷达点云话题转换为pcl点云格式&#xff0c;处理点云后&#xff0c;再次转换为点云话题并发布 主要函数 void laserCloudHandler(const sensor_msgs::PointClo…

Arduino平台ESP8266数据flash存储模式使用介绍

Arduino平台ESP8266数据flash存储模式使用介绍 &#x1f4cc;参考说明文档&#xff1a;https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html PROGMEM关键字&#xff0c;原基于AVR单片机使用的&#xff0c;为了节省RAM空间&#xff0c;将字符串放入闪存中&#xff0…