篇二十一:中介者模式:解耦对象之间的交互

篇二十一:"中介者模式:解耦对象之间的交互"

开始本篇文章之前先推荐一个好用的学习工具,AIRIght,借助于AI助手工具,学习事半功倍。欢迎访问:http://airight.fun/。

另外有2本不错的关于设计模式的资料,分享出来与大家学习参考。
链接:https://pan.baidu.com/s/1RmhQF_o1CdK8U7s5KeILog?pwd=xc6d
提取码:xc6d

设计模式是软件开发中的重要知识,中介者模式(Mediator Pattern)是一种行为型设计模式,用于解耦对象之间的交互,从而减少对象之间的直接依赖,提高系统的灵活性和可维护性。本文将探讨中介者模式的作用和实现方式,并演示在C++中如何应用中介者模式来解耦对象之间的交互。

1. 中介者模式的作用:

中介者模式的作用在于通过引入中介者对象来管理对象之间的交互,使得对象之间不再直接相互通信,而是通过中介者进行通信,从而将系统中对象之间的耦合关系降低到最低。中介者模式包含以下核心角色:

  • 抽象中介者(Abstract Mediator):定义中介者对象的接口,用于管理对象之间的交互。
  • 具体中介者(Concrete Mediator):实现抽象中介者接口,负责协调各个同事类之间的交互关系。
  • 抽象同事类(Abstract Colleague):定义同事类的接口,用于与中介者进行通信。
  • 具体同事类(Concrete Colleague):实现抽象同事类接口,负责与其他同事类进行通信。

中介者模式的关键在于将对象之间的交互逻辑集中到中介者对象中,使得每个对象只关心自身的逻辑,而不需要知道其他对象的存在。

2. 中介者模式的实现方式:

中介者模式的实现方式一般分为以下几个步骤:

  • 定义抽象中介者类:在抽象中介者类中声明用于协调同事对象之间交互的方法。
  • 定义抽象同事类:在抽象同事类中声明用于与中介者通信的方法。
  • 定义具体中介者类:实现抽象中介者类接口,负责协调同事对象之间的交互关系。
  • 定义具体同事类:实现抽象同事类接口,负责与其他同事对象进行通信,并在需要时通过中介者对象进行交互。

3. 在C++中应用中介者模式:

以下是中介者模式的C++示例代码:

a. 定义抽象中介者类:

// Mediator.h
class Colleague;

class Mediator {
    
    
public:
    virtual ~Mediator() {
    
    }
    virtual void sendMessage(const std::string& message, Colleague* colleague) = 0;
};

b. 定义抽象同事类:

// Colleague.h
#include <string>

class Mediator;

class Colleague {
    
    
public:
    Colleague(Mediator* mediator) : mediator_(mediator) {
    
    }
    virtual ~Colleague() {
    
    }
    virtual void receiveMessage(const std::string& message) = 0;
    virtual void sendMessage(const std::string& message) {
    
    
        mediator_->sendMessage(message, this);
    }

protected:
    Mediator* mediator_;
};

c. 定义具体中介者类:

// ConcreteMediator.h
#include <iostream>
#include "Mediator.h"
#include "Colleague.h"

class ConcreteMediator : public Mediator {
    
    
public:
    void sendMessage(const std::string& message, Colleague* colleague) override {
    
    
        if (colleague == colleagueA_) {
    
    
            colleagueB_->receiveMessage(message);
        } else if (colleague == colleagueB_) {
    
    
            colleagueA_->receiveMessage(message);
        }
    }

    void setColleagueA(Colleague* colleagueA) {
    
    
        colleagueA_ = colleagueA;
    }

    void setColleagueB(Colleague* colleagueB) {
    
    
        colleagueB_ = colleagueB;
    }

private:
    Colleague* colleagueA_;
    Colleague* colleagueB_;
};

d. 定义具体同事类:

// ConcreteColleague.h
#include <iostream>
#include "Colleague.h"

class ConcreteColleagueA : public Colleague {
    
    
public:
    ConcreteColleagueA(Mediator* mediator) : Colleague(mediator) {
    
    }

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

class ConcreteColleagueB : public Colleague {
    
    
public:
    ConcreteColleagueB(Mediator* mediator) : Colleague(mediator) {
    
    }

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

e. 客户端使用:

// main.cpp
#include "ConcreteMediator.h"
#include "ConcreteColleague.h"

int main() {
    
    
    ConcreteMediator mediator;
    ConcreteColleagueA colleagueA(&mediator);
    ConcreteColleagueB colleagueB(&mediator);

    mediator.setColleagueA(&colleagueA);
    mediator.setColleagueB(&colleagueB);

    colleagueA.sendMessage("Hello from ConcreteColleagueA!");
    colleagueB.sendMessage("Hi from ConcreteColleagueB!");

    return 0;
}

4. 中介者模式的代码解析:

在中介者模式中,通过引入中介者对象,将对象之间的交互逻辑集中在中介者对象中,从而使得对象之间不再直接相互通信。客户端通过中介者对象来进行对象之间的通信,实现了对象之间的解耦。

5. 最佳实践:

在使用中介者模式时,需要注意以下几点:

  • 合理设计中介者接口:中介者接口应该定义统一的通信方法,确保每个具体同事类都能够通过中介者进行通信。
  • 对象之间的依赖关系:使用中介者模式可能导致中介者对象的职责过重,需要确保中介者对象不会成为过于复杂的"上帝对象"。

**

  1. 总结:**

中介者模式是一种重要的设计模式,它通过引入中介者对象来解耦对象之间的交互,降低系统中对象之间的耦合关系。在C++中,我们可以通过抽象中介者类和具体中介者类来实现中介者模式。中介者模式特别适用于对象之间交互复杂,耦合度较高的场景,能够提高代码的灵活性和可维护性。

希望本文能够帮助您理解中介者模式的作用和实现方式,并通过C++的示例代码演示了如何在C++中应用中介者模式来解耦对象之间的交互。设计模式是软件开发中的重要知识,掌握不同的设计模式有助于提高代码质量、可维护性和可扩展性。

参考文献:

  • Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional.
  • C++ Core Guidelines: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines

感谢您的阅读,欢迎一起探讨,共同进步,推荐大家使用学习助手AIRight解答学习过程中的问题,访问链接:http://airight.fun/

猜你喜欢

转载自blog.csdn.net/Jake_cai/article/details/132144142