• 设计模式——中介者模式


    中介者模式是什么?

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

    中介者模式解决什么问题?

    如没有联合国之前,国与国沟通需要建立联系,当国家增多时,形成网状结构错综复杂,当引入联合国这个中介时,它们就不必直接通信,变为星状结构

    中介者模式实现

    Country类,内部维护UnitedNations,每个子类都有发送和接收信息

    public class Country {
        protected UnitedNations mUnitedNations;
    
        public Country(UnitedNations unitedNations) {
            mUnitedNations = unitedNations;
        }
    }
    
    class USA extends Country {
    
        public USA(UnitedNations unitedNations) {
            super(unitedNations);
        }
    
        public void declare(String msg) {
            mUnitedNations.declare(msg, this);
        }
    
        public void getMessage(String message) {
            System.out.println("USA获取信息: " + message);
        }
    }
    
    class China extends Country {
    
        public China(UnitedNations unitedNations) {
            super(unitedNations);
        }
    
        public void declare(String msg) {
            mUnitedNations.declare(msg, this);
        }
    
        public void getMessage(String message) {
            System.out.println("China获取信息: " + 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
    • 36
    • 37

    UnitedNations作为中介者,具体子类处理信息的传递

    abstract class UnitedNations {
        abstract void declare(String msg, Country country);
    }
    
    class UnitedNationsSecurityCouncil extends UnitedNations {
    
        private USA mUSA;
        private China mChina;
    
        public void setUSA(USA USA) {
            mUSA = USA;
        }
    
        public void setChina(China china) {
            mChina = china;
        }
    
        @Override
        void declare(String msg, Country country) {
            if (country == mUSA) {
                mChina.getMessage(msg);
            } else if (country == mChina) {
                mUSA.getMessage(msg);
            }
        }
    }
    
    • 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

    当使用时

    UnitedNationsSecurityCouncil securityCouncil = new UnitedNationsSecurityCouncil();
    USA usa = new USA(securityCouncil);
    China china = new China(securityCouncil);
    
    securityCouncil.setChina(china);
    securityCouncil.setUSA(usa);
    
    usa.declare("hello");
    china.declare("你好");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    把交互复杂性变为中介者的复杂性,当出现多对多交互时,需先考虑设计是否合理再采用中介者模式

  • 相关阅读:
    【洛谷 P1996】约瑟夫问题 题解(数组+模拟+循环)
    【设计模式】原型模式
    Spring Boot——Thymeleaf
    ORA-30036:无法按8扩展段(在还原表空间‘UNDOTBS1‘中)
    计算机视觉+人工智能面试笔试总结——深度学习基础题41~51
    flink 批处理和流式 wordcount
    webStorm内存溢出问题
    JAVA线程池的使用
    Redis缓存
    VGGNet剪枝实战:使用VGGNet训练、稀疏训练、剪枝、微调等,剪枝出只有3M的模型
  • 原文地址:https://blog.csdn.net/qq_35258036/article/details/133044558