美文网首页
设计模式-中介模式(二十二)

设计模式-中介模式(二十二)

作者: 巨子联盟 | 来源:发表于2018-05-28 20:01 被阅读0次

    中介模式,又叫 调停者模式 Mediator
    包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用,降低耦合

    • 问题:


      image.png
    • 调停者模式效果


      image.png

    鸡蛋都放一个篮子里了??

    1. 物以类聚,人以群分.这个模式分类的度狠重要
    2. 虽然在架构设计里这种方式很危险,但是敲代码的时候能做到这种效果还是很难得的.

    调停者所协调的对象叫同事(Colleague)对象

    • 上类图:
    调停者模式.png

    违反开闭原则,加一个同事要在Mediator里面添加一个属性和对应的操作.

    优点:化繁为简,最大程度的支持了迪米特法则
    缺点:调停者类狠复杂,而且代码很难复用

    设计模式虽好,但不能滥用

    • 代码示例:

    1: 创建一个抽象同事类角色接口

    package com.byedbl.mediator;
    
    /**
     *  An abstract colleague
     */
    public interface Colleague {
        void change();
        void action();
    }
    
    1. 创建3个同事
    package com.byedbl.mediator;
    
    /**
     * A concrete colleague
     */
    
    public class ColleagueA implements Colleague {
        private final String type = "A";
        private Mediator med;
    
        public ColleagueA(Mediator m) {
            med = m;
            med.register(this, type);
        }
    
        @Override
        public void change() {
            System.out.println("-----  A changed now !  -----");
            med.changed(type);
        }
    
        @Override
        public void action() {
            System.out.println("  A is changed by mediator ");
        }
    }
    
    package com.byedbl.mediator;
    
    /**
     * A concrete colleague
     */
    
    public class ColleagueB implements Colleague {
        private final String type = "B";
        private Mediator med;
    
        public ColleagueB(Mediator m) {
            med = m;
            med.register(this, type);
        }
    
        @Override
        public void change() {
            System.out.println("-----  B changed now !  -----");
            med.changed(type);
        }
    
        @Override
        public void action() {
            System.out.println("  B is changed by mediator ");
        }
    }
    
    package com.byedbl.mediator;
    
    /**
     * A concrete colleague
     */
    
    public class ColleagueC implements Colleague {
        private final String type = "C";
        private Mediator med;
    
        public ColleagueC(Mediator m) {
            med = m;
            med.register(this, type);
        }
    
        @Override
        public void change() {
            System.out.println("-----  C changed now !  -----");
            med.changed(type);
        }
    
        @Override
        public void action() {
            System.out.println("  C is changed by mediator ");
        }
    }
    
    1. 定义一个抽象调停者角色
    package com.byedbl.mediator;
    
    /**
     *  An abstract Mediator
     */
    public interface Mediator  {
        public void register(Colleague c, String type);
        public void changed(String type);
    }
    
    1. 实现了一个管理所有的同事的复杂调停者类,
    package com.byedbl.mediator;
    
    /**
     * A concrete mediator
     */
    public class ConcreteMediator implements Mediator {
        private ColleagueA a;
        private ColleagueB b;
        private ColleagueC c;
    
        public ConcreteMediator() {
        }
    
        @Override
        public void register(Colleague colleague, String type) {
            if (type == "A") {
                a = (ColleagueA) colleague;
            } else if (type == "B") {
                b = (ColleagueB) colleague;
            } else if (type == "C") {
                c = (ColleagueC) colleague;
            }
        }
    
        @Override
        public void changed(String type) {
            if (type == "A") {
                b.action();
                c.action();
            } else if (type == "B") {
                a.action();
                c.action();
            } else if (type == "C") {
                a.action();
                b.action();
            }
        }
    }
    
    1. 客户端用法
    package com.byedbl.mediator;
    
    /**
     *  A test client
     */
    public class Test  {
        public static void main(String[] args) {
            Mediator myMed = new ConcreteMediator();
            ColleagueA a = new ColleagueA(myMed);
            ColleagueB b = new ColleagueB(myMed);
            ColleagueC c = new ColleagueC(myMed);
    
            a.change();
            System.out.println("----------------");
    
            b.change();
            System.out.println("****************");
            c.change();
        }
    }
    

    这个模式的最大问题是调停者,正所谓成也调停败也调停.管的多,知道的太多也是缺点.

    相关文章

      网友评论

          本文标题:设计模式-中介模式(二十二)

          本文链接:https://www.haomeiwen.com/subject/nsqnjftx.html