墨风如雪博客

  • 源码小店
  • 导航站
  • 登录
  • java
  • 资源分享
让AI使用变得如此简单
  1. 首页
  2. java
  3. 设计模式
  4. 正文

设计模式:中介者设计模式

2023年 5月 13日 159点热度 0人点赞 0条评论

中介者设计模式

中介者设计模式是一种行为型设计模式,它通过一个中介者对象来封装一系列对象之间的交互关系,从而降低对象之间的耦合度。中介者对象充当了所有对象之间的协调者,它负责处理对象之间的通信和控制对象之间的行为。

优点:

  1. 降低耦合度:中介者对象将对象之间的交互关系封装起来,从而减少了对象之间的直接依赖关系,降低了耦合度,使得对象之间更加灵活和可扩展。

  2. 集中控制逻辑:中介者对象集中了对象之间的通信和行为控制逻辑,使得系统更加清晰和易于维护。

  3. 代码复用:中介者对象可以被多个对象共享和重用,从而减少了代码的冗余和重复。

缺点:

  1. 中介者对象过于复杂:中介者对象可能会变得非常复杂,因为它需要处理所有对象之间的通信和行为控制逻辑,从而增加了代码的维护成本和开发难度。

  2. 性能问题:中介者对象可能会成为系统的瓶颈,因为它需要处理所有对象之间的通信和行为控制逻辑,从而增加了系统的开销和响应时间。

下面是一个简单的 Java 代码示例,展示了如何使用中介者设计模式来协调多个对象之间的交互关系:

// 中介者接口
public interface Mediator {
    void notify(Component sender, String event);
}

// 具体中介者实现类
public class ConcreteMediator implements Mediator {
    private ComponentA componentA;
    private ComponentB componentB;

    public void setComponentA(ComponentA componentA) {
        this.componentA = componentA;
    }

    public void setComponentB(ComponentB componentB) {
        this.componentB = componentB;
    }

    @Override
    public void notify(Component sender, String event) {
        if (sender == componentA) {
            System.out.println("Component A triggered event: " + event);
            componentB.doSomething();
        } else if (sender == componentB) {
            System.out.println("Component B triggered event: " + event);
            componentA.doSomething();
        }
    }
}

// 抽象组件类
public abstract class Component {
    protected Mediator mediator;

    public Component(Mediator mediator) {
        this.mediator = mediator;
    }

    public void triggerEvent(String event) {
        mediator.notify(this, event);
    }

    public abstract void doSomething();
}

// 具体组件A实现类
public class ComponentA extends Component {
    public ComponentA(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void doSomething() {
        System.out.println("Component A do something.");
    }
}

// 具体组件B实现类
public class ComponentB extends Component {
    public ComponentB(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void doSomething() {
        System.out.println("Component B do something.");
    }
}

在上面的例子中,我们定义了一个中介者接口 Mediator,它有一个 notify 方法,用于处理组件之间的通信和行为控制逻辑。然后,我们创建了一个具体中介者实现类 ConcreteMediator,它维护了两个组件对象 componentA 和 componentB,并实现了 Mediator 接口的 notify 方法,用于处理组件之间的通信和行为控制逻辑。

接着,我们定义了一个抽象组件类 Component,它有一个 triggerEvent 方法,用于触发事件,并将事件通知给中介者对象。具体组件类 ComponentA 和 ComponentB 分别继承了抽象组件类 Component,并实现了 doSomething 方法,用于执行具体的业务逻辑。

最后,我们在客户端代码中创建了一个中介者对象 mediator 和两个组件对象 componentA 和 componentB,并将它们之间的关系交给中介者对象进行协调。当组件对象 componentA 触发事件时,中介者对象 mediator 会接收到事件,并将事件通知给组件对象 componentB,从而协调它们之间的交互关系。当组件对象 componentB 触发事件时,中介者对象 mediator 会接收到事件,并将事件通知给组件对象 componentA,从而协调它们之间的交互关系。

jdk当中的使用

中介者模式在实际项目中的应用非常广泛,例如在电商平台中,订单服务作为中介者对象,协调买家服务和卖家服务之间的交互关系,从而实现订单的创建、支付、发货和确认等功能。又如,在游戏开发中,游戏场景作为中介者对象,协调各个游戏对象之间的交互关系,从而实现游戏的运行和逻辑控制。

下面是一个简单的 Java 代码示例,展示了如何使用中介者模式来协调多个对象之间的交互关系:

// 中介者接口
public interface Mediator {
    void send(String message, Colleague colleague);
}

// 抽象同事类
public abstract class Colleague {
    protected Mediator mediator;

    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }

    public abstract void receive(String message);
    public abstract void send(String message);
}

// 具体同事类A
public class ConcreteColleagueA extends Colleague {
    public ConcreteColleagueA(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void receive(String message) {
        System.out.println("ConcreteColleagueA received message: " + message);
    }

    @Override
    public void send(String message) {
        mediator.send(message, this);
    }
}

// 具体同事类B
public class ConcreteColleagueB extends Colleague {
    public ConcreteColleagueB(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void receive(String message) {
        System.out.println("ConcreteColleagueB received message: " + message);
    }

    @Override
    public void send(String message) {
        mediator.send(message, this);
    }
}

// 具体中介者类
public class ConcreteMediator implements Mediator {
    private ConcreteColleagueA colleagueA;
    private ConcreteColleagueB colleagueB;

    public void setColleagueA(ConcreteColleagueA colleagueA) {
        this.colleagueA = colleagueA;
    }

    public void setColleagueB(ConcreteColleagueB colleagueB) {
        this.colleagueB = colleagueB;
    }

    @Override
    public void send(String message, Colleague colleague) {
        if (colleague == colleagueA) {
            colleagueB.receive(message);
        } else if (colleague == colleagueB) {
            colleagueA.receive(message);
        }
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        ConcreteMediator mediator = new ConcreteMediator();
        ConcreteColleagueA colleagueA = new ConcreteColleagueA(mediator);
        ConcreteColleagueB colleagueB = new ConcreteColleagueB(mediator);
        mediator.setColleagueA(colleagueA);
        mediator.setColleagueB(colleagueB);
        colleagueA.send("Hello, I am Colleague A.");
        colleagueB.send("Hello, I am Colleague B.");
    }
}

在上面的例子中,我们定义了一个中介者接口 Mediator,它有一个 send 方法,用于处理同事对象之间的通信和协调关系。然后,我们创建了两个具体同事类 ConcreteColleagueA 和 ConcreteColleagueB,它们分别继承了抽象同事类 Colleague,并实现了 receive 方法和 send 方法,用于执行具体的业务逻辑。

接着,我们定义了一个具体中介者类 ConcreteMediator,它维护了两个同事对象 colleagueA 和 colleagueB,并实现了 Mediator 接口的 send 方法,用于处理同事对象之间的通信和协调关系。具体地,当同事对象 colleagueA 发送消息时,中介者对象 mediator 会将消息转发给同事对象 colleagueB;当同事对象 colleagueB 发送消息时,中介者对象 mediator 会将消息转发给同事对象 colleagueA。

最后,我们在客户端代码中创建了一个中介者对象 mediator 和两个同事对象 colleagueA 和 colleagueB,并将同事对象的中介者属性设置为中介者对象 mediator。当同事对象 colleagueA 和 colleagueB 分别发送消息时,中介者对象 mediator 会接收到消息,并将消息转发给对应的同事对象,从而协调它们之间的交互关系。

上述代码示例是一个简单的中介者模式的实现,它将同事对象之间的交互关系解耦,通过中介者对象来实现同事对象之间的通信和协调关系。在实际应用中,我们可以根据具体的业务需求和系统架构,使用中介者模式来协调多个对象之间的交互关系,从而实现系统的高性能、高可扩展性和高可维护性。

设计模式的过度使用

当系统中存在大量的对象需要进行交互时,过度使用中介者模式会引发以下问题:

  1. 代码复杂性增加:如果每个对象都通过中介者对象进行交互,中介者对象的复杂度会随着对象数量的增加而增加,导致代码复杂性增加,难以维护和扩展。

  2. 系统性能降低:中介者对象会成为系统的瓶颈,导致系统性能降低。

  3. 系统耦合度增加:使用中介者模式解耦对象之间的交互关系,会把所有交互关系都集中在中介者对象中,导致中介者对象和其他对象之间的依赖关系增加,系统耦合度增加。

因此,在使用中介者模式时,需要根据实际情况来决定是否使用,并注意控制中介者对象的复杂度和性能,避免过度使用中介者模式导致代码复杂性增加、系统性能降低和耦合度增加等问题。

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: java 中介者设计模式 教程 设计模式
最后更新:2023年 5月 11日

墨风如雪

一个热爱生活,热爱分享的程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

墨风如雪

一个热爱生活,热爱分享的程序员

最新 热点 随机
最新 热点 随机
告别机械感!OpenAudio S1让AI声音活起来 Sora触手可及!微软必应AI视频生成器,全民创作时代来临? 阿里WebAgent开源:引领自主搜索新纪元 重磅炸弹!字节跳动开源BAGEL:70亿参数,统一多模态理解与生成,AI“全能王”诞生记! 小米MiMo-VL:7B参数,怎么就成了多模态界的“越级打怪王”? 炸裂!DeepSeek 8B 量化版降临:告别显存焦虑,你的 3080 Ti 也能玩转顶级大模型了!
AI圈炸锅了!Mistral Medium 3:性能 SOTA,成本打骨折,企业玩家的新宠?字节终于开源“扣子”同款引擎了!FlowGram:AI 时代的可视化工作流利器告别“微信黑箱”!Chatlog:让你的聊天记录也能拥有“AI大脑”!字节跳动 Seed-Coder-8B:不靠人工洗数据,这80亿参数的小模型如何写出顶尖代码?85倍速的视觉革命:苹果发布 FastVLM,让你的 iPhone ‘看图说话’,快到飞起!告别AI视频“变脸怪”!腾讯混元Hunyuan Custom重磅开源,主体一致性“王炸”来了!
DeepWiki 开源版本:AI 帮你自动写代码 Wiki,告别手动苦海! Google AI Studio免费开放Gemini 2.0 Flash Experimental画图模型:一场创意设计的革命 前端 Vue 基础知识 震撼发布!RF-DETR:60.5 mAP + 6ms延迟,实时检测领域的新王者如何碾压YOLO? 探索AI编程的边界:GPT、Gemini、DeepSeek三巨头过招,谁能更胜一筹? java Web框架Spring MVC的(超详细总结)
标签聚合
deepseek 教程 设计模式 动态规划 java spring 算法 AI

COPYRIGHT © 2023 墨风如雪博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

免责声明 - 隐私政策