• 设计模式之中介者模式


    自1945年成立了联合国之后,地球上就没有发生世界范围的战争,可以说联合国对世界和平的作用不可估量。它就是一个调停者、中介者角色,协调了各国之间的利益冲突与摩擦,解决或缓解了国际间经济、社会、文化和人道主义性质的问题。不同的对象与对象之间的关系,就如同国与国之间的关系错综复杂,有些情况下也需要一个“联合国”。

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

    代码模拟各国通过安理会交流信息:

    //联合国机构类,相当于mediator类

    public abstract class UnitedNations {
        public abstract void declare(String message, Country colleague);
    }

    //国家类相当于Colleague类

    public class Country {
        protected UnitedNations mediator;
        public Country(UnitedNations mediator){
            this.mediator = mediator;
        }
    }
    public class USA extends Country{
        public USA(UnitedNations mediator){
            super(mediator);
        }
        public void declare(String msg){
            mediator.declare(msg, this);
        }
        public void getMessage(String message){
            System.out.println("美国获得对方信息:" + message);
        }
    }
    public class Iraq extends Country{
        public Iraq(UnitedNations mediator){
            super(mediator);
        }
        public void declare(String msg){
            mediator.declare(msg, this);
        }
        public void getMessage(String message){
            System.out.println("伊拉克获得对方信息:" + message);
        }
    }
    public class UnitedSecurityCouncil extends UnitedNations{
        private USA colleague1;
        private Iraq colleague2;
        @Override
        public void declare(String message, Country colleague) {
            // TODO Auto-generated method stub
            if(colleague == colleague1){
                colleague2.getMessage(message);
            }else{
                colleague1.getMessage(message);
            }
        }
        public USA getColleague1() {
            return colleague1;
        }
        public void setColleague1(USA colleague1) {
            this.colleague1 = colleague1;
        }
        public Iraq getColleague2() {
            return colleague2;
        }
        public void setColleague2(Iraq colleague2) {
            this.colleague2 = colleague2;
        }
    
    }
    public class Test1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            UnitedSecurityCouncil m = new UnitedSecurityCouncil();
            USA c1 = new USA(m);
            Iraq c2 = new Iraq(m);
            
            m.setColleague1(c1);
            m.setColleague2(c2);
            
            c1.declare("chilemei?");
            c2.declare("chiguole");
        }
    
    }

    中介者模式优点:首先Mediator的出现减少了各个Colleague之间的耦合,使得可以独立地改变和复用各个Colleague类和Mediator;其次由于把对象如何协作进行了抽象,将中介者作为一个独立概念并封装在一个对象当中,这样关注的对象就从对象各自本身的行为转移到它们之间的交互上来了,也就是站在一个更宏观的角度去看待系统。不过这样也把对象间交互的复杂性变为中介者的复杂性,一旦中介者出问题,就会影响很多类。所以当系统中出现“多对多”交互复杂的对象群时,不要着急使用中介者模式,而应该反思设计是否合理。

  • 相关阅读:
    【上线复盘】20190329-一个刷新数据的接口是如何导致几十万的订单数据错误
    VS------csc.exe已停止工作解决方法
    SQLServer------存储过程的使用
    SQLServer------聚集索引和非聚集索引的区别
    SQLServer------Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON
    SQLServer------如何快速插入几万条测试数据
    SQLServer------如何让标识列重新开始计算
    SQLServer------begin tran/commit tran事务的使用方法
    SQLServer------插入数据时出现IDENTITY_INSERT错误
    Spring----Spring Boot Rest的使用方法
  • 原文地址:https://www.cnblogs.com/shicaiyou/p/9361629.html
Copyright © 2020-2023  润新知