当前位置: 首页 > article >正文

JS设计模式之中介者模式

前言

中介者模式(Mediator Pattern)是一种行为设计模式,旨在通过引入一个中介者对象来减少多个对象之间的直接交互,降低对象之间的耦合度。

这种模式使得对象之间的通信通过中介者来协调,从而减少了对象之间的直接依赖。

主要组成部分

中介者模式包含以下几个核心组件:

  1. 中介者(Mediator):定义一个接口,该接口用于与各个具体同事对象进行交互,中介者负责管理这些同事对象之间的通信。

  2. 具体中介者(Concrete Mediator):实现中介者接口,维护对具体同事对象的引用,并实现协调它们之间交互的具体逻辑。

  3. 同事(Colleague):定义一个接口,表示参与交互的对象。每个同事对象都持有对中介者的引用,并通过中介者与其他同事对象进行交互。

  4. 具体同事(Concrete Colleague):实现同事接口,具体的同事对象通过中介者与其他同事对象进行通信。

工作原理

客户端创建一个具体中介者和多个同事对象。同事对象通过中介者进行交互,而不是直接引用其他同事对象。中介者协调同事对象之间的通信,处理它们之间的交互逻辑。

优点

  • 降低耦合度:通过中介者对象,减少了同事对象之间的直接依赖,使得系统更加灵活和可维护。
  • 集中控制:中介者集中管理同事对象之间的交互逻辑,便于修改和扩展。

缺点

  • 中介者复杂性:中介者可能会变得复杂,尤其是在同事对象较多时,可能导致中介者的职责过于繁重。

  • 不易扩展:如果需要添加新的同事对象,可能需要修改中介者的实现,增加了维护成本。

应用场景

中介者模式适用于多种场景,包括但不限于:

  1. GUI组件交互
  2. 聊天应用
  3. 订单处理系统
  4. 游戏开发
  5. 工作流管理
  6. 网络协议
  7. 多模块系统

示例

以下是中介者模式的一个简单实现示例,展示了如何通过中介者来协调两个对象之间的交互:

// 中介者接口  
class Mediator {  
    notify(colleague, event) {}  
}  

// 具体中介者  
class ConcreteMediator extends Mediator {  
    constructor(colleague1, colleague2) {  
        super();  
        this.colleague1 = colleague1;  
        this.colleague1.setMediator(this);  
        this.colleague2 = colleague2;  
        this.colleague2.setMediator(this);  
    }  

    notify(colleague, event) {  
        if (event === 'A') {  
            console.log('Mediator reacts on A and triggers B');  
            this.colleague2.doSomethingB();  
        } else if (event === 'B') {  
            console.log('Mediator reacts on B and triggers A');  
            this.colleague1.doSomethingA();  
        }  
    }  
}  

// 同事接口  
class Colleague {  
    setMediator(mediator) {  
        this.mediator = mediator;  
    }  
}  

// 具体同事A  
class ConcreteColleagueA extends Colleague {  
    doSomethingA() {  
        console.log('Colleague A does something.');  
        this.mediator.notify(this, 'A');  
    }  
}  

// 具体同事B  
class ConcreteColleagueB extends Colleague {  
    doSomethingB() {  
        console.log('Colleague B does something.');  
        this.mediator.notify(this, 'B');  
    }  
}  

// 使用中介者模式  
const colleagueA = new ConcreteColleagueA();  
const colleagueB = new ConcreteColleagueB();  
const mediator = new ConcreteMediator(colleagueA, colleagueB);  

colleagueA.doSomethingA(); // Colleague A does something. Mediator reacts on A and triggers B  
colleagueB.doSomethingB(); // Colleague B does something. Mediator reacts on B and triggers A

总结

中介者模式提供了一种有效的方式来降低系统中对象之间的耦合度,通过集中管理对象间的交互逻辑,使得系统更加模块化和易于维护。

尽管在某些情况下,中介者可能会变得复杂,且不易扩展,但在需要减少对象间直接交互的场景中,中介者模式是一个非常有用的设计模式。

– 欢迎点赞、关注、转发、收藏【我码玄黄】,各大平台同名。


http://www.kler.cn/a/440887.html

相关文章:

  • 数字信号处理--过采样
  • 如何评估并持续优化AI呼出机器人的使用效果
  • 【大模型量化】GPTQ量化模型
  • Cookie,Seesion和Token区别及用途
  • 概率论得学习和整理29: 用EXCEL 描述二项分布
  • 餐饮喜好及健康饮食推荐小程序
  • CVE-2024-32709 WordPress —— Recall 插件存在 SQL 注入漏洞
  • 前端发展前景探讨:技术进步与职业发展机会
  • 大数据面试题--企业面试真题
  • cephFS的使用以及K8S对接cephFS
  • 构建智能化教育平台:知识中台在教育行业的创新应用
  • 路由传值的几种方式
  • 数据结构——图论基础
  • 开源的工作流编排工具—Airflow
  • 一个串口通讯的例子(ABX PENTRA 120)
  • 第三篇:HTTP 的烦恼与进化史
  • 《解析 MXNet 的 C++版本在分布式训练中的机遇与挑战》
  • 用Disk Genius备份和还原硬盘
  • Git Bash Here 中文显示乱码的处理方法
  • springboot中——Logback介绍