设计模式(023)行为型之中介者模式

2024-04-17 11:12

本文主要是介绍设计模式(023)行为型之中介者模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        中介者模式是一种行为型设计模式,用于减少对象之间的直接通信,而是通过一个中介对象来进行间接通信。这种模式有助于减少对象之间的耦合,使得系统更易于维护和扩展。
在中介者模式中,主要有以下几个角色:
① 中介者(Mediator):定义了一个接口用于各个同事对象之间的通信,并负责实现具体的协调逻辑。
② 具体中介者(ConcreteMediator):实现了中介者接口,负责协调各个同事对象之间的通信。
③ 同事类(Colleague):每个同事类都知道中介者对象,并通过中介者对象来通信,而不是直接与其他同事对象通信。
④ 具体同事类(ConcreteColleague):实现了同事类接口,每个具体同事类都需要知道中介者对象,并通过中介者对象来进行通信。
中介者模式通常应用于多个对象之间存在复杂的交互关系,通过引入中介者对象来简化对象之间的通信,降低耦合度,同时也使得系统更易于理解和扩展。

1、场景设计

实现场景:A、B两个同事通过中介者进行消息收发。

2、C++实现

`Colleague` 是同事类接口,定义了同事对象的基本行为。`ConcreteColleagueA` 和 `ConcreteColleagueB` 是具体的同事类,实现了同事类接口。`Mediator` 是中介者类接口,定义了中介者对象的基本行为。`ConcreteMediator` 是具体的中介者类,负责协调不同同事类之间的通信。在 `main` 函数中,我们创建了中介者对象和两个具体同事对象,并设置了中介者对象的引用,然后通过同事对象发送消息,中介者对象负责转发消息给其他同事对象。 

#include <iostream>
#include <string>
#include <vector>// 前置声明
class Mediator;// 同事类接口
class Colleague {protected:Mediator* mediator;public:Colleague(Mediator* mediator) : mediator(mediator) {}virtual void send(const std::string& message) = 0;virtual void receive(const std::string& message) = 0;
};// 中介者类接口
class Mediator {public:virtual void send(const std::string& message, Colleague* colleague) = 0;
};// 具体同事类A
class ConcreteColleagueA : public Colleague {public:ConcreteColleagueA(Mediator* mediator) : Colleague(mediator) {}void send(const std::string& message) override {mediator->send(message, this);}void receive(const std::string& message) override {std::cout << "ConcreteColleagueA received: " << message << std::endl;}
};// 具体同事类B
class ConcreteColleagueB : public Colleague {public:ConcreteColleagueB(Mediator* mediator) : Colleague(mediator) {}void send(const std::string& message) override {mediator->send(message, this);}void receive(const std::string& message) override {std::cout << "ConcreteColleagueB received: " << message << std::endl;}
};// 具体中介者类
class ConcreteMediator : public Mediator {private:ConcreteColleagueA* colleagueA;ConcreteColleagueB* colleagueB;public:void setColleagueA(ConcreteColleagueA* colleagueA) {this->colleagueA = colleagueA;}void setColleagueB(ConcreteColleagueB* colleagueB) {this->colleagueB = colleagueB;}void send(const std::string& message, Colleague* colleague) override {if (colleague == colleagueA) {colleagueB->receive(message);} else if (colleague == colleagueB) {colleagueA->receive(message);}}
};int main() {ConcreteMediator mediator;ConcreteColleagueA colleagueA(&mediator);ConcreteColleagueB colleagueB(&mediator);mediator.setColleagueA(&colleagueA);mediator.setColleagueB(&colleagueB);colleagueA.send("Hello from colleague A");colleagueB.send("Hi from colleague B");return 0;
}

3、Java实现

`Mediator` 是中介者接口,定义了中介者对象的基本行为。`Colleague` 是同事类抽象类,定义了同事对象的基本行为。`ConcreteColleagueA` 和 `ConcreteColleagueB` 是具体的同事类,实现了同事类抽象类。`ConcreteMediator` 是具体的中介者类,负责协调不同同事类之间的通信。在 `main` 方法中,我们创建了中介者对象和两个具体同事对象,并将同事对象添加到中介者对象中,然后通过同事对象发送消息,中介者对象负责转发消息给其他同事对象。 

package behavioralpattern.mediator;import java.util.ArrayList;
import java.util.List;// 中介者接口
interface Mediator {void sendMessage(String message, Colleague colleague);
}// 同事类抽象类
abstract class Colleague {protected Mediator mediator;public Colleague(Mediator mediator) {this.mediator = mediator;}public abstract void sendMessage(String message);public abstract void receiveMessage(String message);
}// 具体同事类A
class ConcreteColleagueA extends Colleague {public ConcreteColleagueA(Mediator mediator) {super(mediator);}@Overridepublic void sendMessage(String message) {mediator.sendMessage(message, this);}@Overridepublic void receiveMessage(String message) {System.out.println("ConcreteColleagueA received: " + message);}
}// 具体同事类B
class ConcreteColleagueB extends Colleague {public ConcreteColleagueB(Mediator mediator) {super(mediator);}@Overridepublic void sendMessage(String message) {mediator.sendMessage(message, this);}@Overridepublic void receiveMessage(String message) {System.out.println("ConcreteColleagueB received: " + message);}
}// 具体中介者类
class ConcreteMediator implements Mediator {private List<Colleague> colleagues = new ArrayList<>();public void addColleague(Colleague colleague) {colleagues.add(colleague);}@Overridepublic void sendMessage(String message, Colleague colleague) {for (Colleague c : colleagues) {if (c != colleague) {c.receiveMessage(message);}}}
}public class MediatorDemo {public static void main(String[] args) {ConcreteMediator mediator = new ConcreteMediator();ConcreteColleagueA colleagueA = new ConcreteColleagueA(mediator);ConcreteColleagueB colleagueB = new ConcreteColleagueB(mediator);mediator.addColleague(colleagueA);mediator.addColleague(colleagueB);colleagueA.sendMessage("Hello from colleague A");colleagueB.sendMessage("Hi from colleague B");}
}

这篇关于设计模式(023)行为型之中介者模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/911637

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

迭代器模式iterator

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/iterator 不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素