元月's blog 元月's blog
首页
  • 基础
  • 并发编程
  • JVM
  • Spring
  • Redis篇
  • Nginx篇
  • Kafka篇
  • Otter篇
  • Shardingsphere篇
  • 设计模式
  • MySQL
  • Oracle
  • 基础
  • 操作系统
  • 网络
  • 数据结构
  • 技术文档
  • Git常用命令
  • GitHub技巧
  • 博客搭建
  • 开发工具
更多

元月

临渊羡鱼,不如退而结网
首页
  • 基础
  • 并发编程
  • JVM
  • Spring
  • Redis篇
  • Nginx篇
  • Kafka篇
  • Otter篇
  • Shardingsphere篇
  • 设计模式
  • MySQL
  • Oracle
  • 基础
  • 操作系统
  • 网络
  • 数据结构
  • 技术文档
  • Git常用命令
  • GitHub技巧
  • 博客搭建
  • 开发工具
更多
  • 设计模式

    • 设计模式简介
    • 设计模式之单例模式
    • 设计模式之工厂模式
    • 设计模式之原型模式
    • 设计模式之建造者模式
    • 设计模式之适配器模式
    • 设计模式之桥接模式
    • 设计模式之组合模式
    • 设计模式之装饰器模式
    • 设计模式之外观模式
    • 设计模式之享元模式
    • 设计模式之代理模式
    • 设计模式之责任链模式
    • 设计模式之命令模式
    • 设计模式之解释器模式
    • 设计模式之迭代器模式
    • 设计模式之中介者模式
      • 一、简介
      • 二、实现方式
      • 三、应用场景
        • 1、SpringMVC中的的中介者模式:前置控制器DispatcherServlet
      • 四、思维导图
    • 设计模式之备忘录模式
    • 设计模式之观察者模式
    • 设计模式之状态模式
    • 设计模式之策略模式
    • 设计模式之模版模式
    • 设计模式之访问者模式
    • 设计模式辨析篇
  • 高可用

  • 系统设计
  • 设计模式
元月
2022-08-19
目录

设计模式之中介者模式

# 设计模式之中介者模式

# 一、简介

用一个中介对象来封装一系列的对象交互,来避免对象之间的直接交互

# 二、实现方式

  • Mediator(抽象中介者):用来定义参与者与中介者之间的交互方式

    // 抽象中介者
    public interface Mediator {
        // 定义处理逻辑
        void doEvent(Colleague colleague);
    }
    
    1
    2
    3
    4
    5
  • ConcreteMediator(具体中介者):实现了Mediator。持有参与者对象的集合,管理和通知其它参与者

    // 具体中介者
    @Component
    public class DispatchCenter implements Mediator {
      // 管理有哪些参与者
        @Autowired
        private List<Colleague> colleagues;
      
        @Override
        public void doEvent(Colleague colleague) {
            for(Colleague colleague1 :colleagues){
                if(colleague1==colleague){
                    // 如果是本身高铁信息,可以处理其他的业务逻辑
                    // doSomeThing();
                    continue;
                }
              // 通知其他参与
                colleague1.message();
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
  • Colleague(抽象参与者)

    // 抽象参与者, 也可以使用abstract 写法
    public interface Colleague {
       // 沟通消息
        void message();
    }
    
    1
    2
    3
    4
    5
  • ConcreteColleague(具体参与者)

    // 具体参与者
    @Component
    public class MotorCarOneColleague implements Colleague {
    
        @Override
        public void message() {
            // 模拟处理业务逻辑
            System.out.println("高铁一号收到消息!!!");
        }
    }
    @Component
    public class MotorCarTwoColleague implements Colleague {
        @Override
        public void message() {
            System.out.println("高铁二号收到消息!!!");
        }
    }
    @Component
    public class MotorCarThreeColleague implements Colleague {
        @Override
        public void message() {
            System.out.println("高铁三号收到消息!!!");
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
  • 测试demo

// 测试demo
public static void main(String[] args) {
     // 初始化spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
     // 获取中介者,调度中心
        DispatchCenter dispatchCenter = (DispatchCenter) applicationContext.getBean("dispatchCenter");


        // 一号高铁 发送消息出去
        MotorCarOneColleague motorCarOneColleague =  (MotorCarOneColleague) applicationContext.getBean("motorCarOneColleague");
     // 通过调度中心沟通信息
        dispatchCenter.doEvent(motorCarOneColleague);
        // result:高铁三号收到消息!!!
        //         高铁二号收到消息!!!


        // 二号高铁 发送消息出去
        MotorCarTwoColleague  motorCarTwoColleague = (MotorCarTwoColleague)applicationContext.getBean("motorCarTwoColleague");
        dispatchCenter.doEvent(motorCarTwoColleague);
        // result:高铁一号收到消息!!!
        //         高铁三号收到消息!!!

    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 三、应用场景

# 1、SpringMVC中的的中介者模式:前置控制器DispatcherServlet

# 四、思维导图

#设计模式
设计模式之迭代器模式
设计模式之备忘录模式

← 设计模式之迭代器模式 设计模式之备忘录模式→

最近更新
01
otter二次开发-支持按目标端主键索引Load数据
08-03
02
mvnw简介
06-21
03
gor流量复制工具
06-03
更多文章>
Theme by Vdoing | Copyright © 2022-2024 元月 | 粤ICP备2022071877号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式