上海建网站计划,陕西网络公司,站长网站素材,网站建设常出现的问题篇二十一#xff1a;中介者模式#xff1a;解耦对象之间的交互
开始本篇文章之前先推荐一个好用的学习工具#xff0c;AIRIght#xff0c;借助于AI助手工具#xff0c;学习事半功倍。欢迎访问#xff1a;http://airight.fun/。
另外有2本不错的关于设计模式…篇二十一中介者模式解耦对象之间的交互
开始本篇文章之前先推荐一个好用的学习工具AIRIght借助于AI助手工具学习事半功倍。欢迎访问http://airight.fun/。
另外有2本不错的关于设计模式的资料分享出来与大家学习参考。 链接https://pan.baidu.com/s/1RmhQF_o1CdK8U7s5KeILog?pwdxc6d 提取码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 stringclass 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.hclass 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.hclass 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.hint 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. 最佳实践
在使用中介者模式时需要注意以下几点
合理设计中介者接口中介者接口应该定义统一的通信方法确保每个具体同事类都能够通过中介者进行通信。对象之间的依赖关系使用中介者模式可能导致中介者对象的职责过重需要确保中介者对象不会成为过于复杂的上帝对象。
**
总结**
中介者模式是一种重要的设计模式它通过引入中介者对象来解耦对象之间的交互降低系统中对象之间的耦合关系。在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/