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


    1、中介者模式介绍

    中介者模式(Mediator),用一个中介对象来封装一系列的对象交互。中介者使个对象不需要显式的互相引用,从而使其松耦合,而且可以独立地改变它们之间的交互

    中介者模式很好的体现了迪米特法则,如果两个类不必彼此直接通信,那个这两个类就不应当发生直接的相互作用,如果一个类要调用另一个类的某个方法,可以通过第三者来转发这个调用。

    2、中介者模式结构

    在这里插入图片描述

    通过中介者对象,可以将相互调用的类之间的网状结构,变成以中介者为中心的星型结构,每个具体对象通过中介者与其他对象发生相互作用

    中介者模式角色:

    • Colleague:抽象同事类
    • ConcreteColleague:具体同事类,每个具体同事只知道自己的行为,而不了解其它同事类的情况,但他们都认识中介者对象
    • Mediator:抽象中介者,定义了同事对象到中介者对象的接口
    • ConcreteMediator:具体中介者对象,实现抽象类的方法,需要知道所有的具体同事类,并从具体同事接收消息,向具体同事对象发出命令
    Mediator
    /**
     * 中介者
     */
    public interface Mediator {
    
        /**
         * 发送消息给具体同事
         */
        void sendMessage(String message, Colleague colleague);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    Colleague
    /**
     * 同事类
     */
    public interface Colleague {
    
        /**
         * 发送消息
         */
        void send(String message, Mediator mediator);
    
        /**
         * 接收中介者的消息
         */
        void notify(String message);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    ConcreteMediator
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 具体中介者
     */
    @Service
    public class ConcreteMediator implements Mediator {
    
        /**
         * 管理具体的同事类
         */
        @Resource
        List<Colleague> colleagueList = new ArrayList<>();
    
        @Override
        public void sendMessage(String message, Colleague colleague) {
            for (Colleague col : colleagueList) {
                if (col == colleague) {
                    // 对于自身的消息可以做其它逻辑处理
                    // doSomething...
                    continue;
                }
                // 其它参与者通知
                col.notify(message);
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    ConcreteColleague
    import org.springframework.stereotype.Service;
    
    /**
     * 具体同事 A
     */
    @Service("colleagueA")
    public class ConcreteColleagueA implements Colleague {
    
        public void send(String message, Mediator mediator) {
            mediator.sendMessage(message, this);
        }
    
        public void notify(String message) {
            System.out.println("ConcreteColleagueA 得到消息 " + message);
        }
    
    }
    
    
    /**
     * 具体同事 B
     */
    @Service("colleagueB")
    public class ConcreteColleagueB implements Colleague {
    
        public void send(String message, Mediator mediator) {
            mediator.sendMessage(message, this);
        }
    
        public void notify(String message) {
            System.out.println("ConcreteColleagueB 得到消息 " + message);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    调用方:
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ApplicationTest {
    
        @Resource(name = "colleagueA")
        private Colleague concreteColleagueA;
    
        @Resource(name = "colleagueB")
        private Colleague concreteColleagueB;
    
        @Resource
        private Mediator mediator;
    
        @Test
        public void test() {
            // 具体同事 A 发送消息
            mediator.sendMessage("明天吃什么?", concreteColleagueA);
            // result:ConcreteColleagueB 得到消息 明天吃什么?
    
            // 具体同事 B 发送消息
            mediator.sendMessage("明天吃面吧。", concreteColleagueB);
            // result:ConcreteColleagueA 得到消息 明天吃面吧。
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3、中介者模式优缺点

    1. 中介者模式减少了各个 Colleague 之间的耦合,而且将中介作为一个独立的概念封装到一个对象中,关注的对象就会从对象本身的行为转移到他们之间的交互上来。
    2. 交互由各个类之间集中到了具体中介者对象中,使交互复杂性变为了中介者的复杂性,使中介者变得比任何一个 ConcreteColleague 都复杂,使用中介者模式时要考虑到对象之间交互的复杂度和中介类本身的复杂度
    3. 中介者模式设计思想通过引入中间层,将一组对象之间的交互关系从网状(多对多)转换为星状(一对多),减少了对象之间的交互关系,降低了代码的复杂度,提高了代码的可读性和可维护性。
  • 相关阅读:
    【Linux 安装Kibana 及 Es 分词器安装】
    JAVA开发(关于写代码与数学)
    Android studio TextView的 用法详情
    SSH服务
    VB.net:VB.net编程语言学习之基于VS软件利用VB.net语言实现对CAD/VRML进行二次开发的简介、案例应用之详细攻略
    GeoServer地图服务器权限控制
    NC45 实现二叉树先序,中序和后序遍历
    那些程序员需要高频使用的神器
    Python使用yield协程实现生产者消费者问题
    Linux--共享内存
  • 原文地址:https://blog.csdn.net/qq_53316135/article/details/127606124