zoukankan      html  css  js  c++  java
  • 14.中介者模式

    中介者模式:

    如果一个系统中,对象与对象之间的关系呈网状结构,对象与对象之间存在大量多对多关系,这些对象称为“同事对象”。引入一个中介者对象,通过该对象与其他同事对象打交道,将复杂的网状结构变成星型结构。

    类图:

    //中介者抽象类
    public interface Mediator {
        public void register(String dname,Department d);
        public void commands(String dname);
    }
    
    public class Persident implements Mediator{
        private Map<String, Department> map = new HashMap<String, Department>();
        @Override
        public void register(String dname, Department d) {
            // TODO Auto-generated method stub
            map.put(dname, d);
        }
    
        @Override
        public void commands(String dname) {
            // TODO Auto-generated method stub
            map.get(dname).selfAction();
        }
    }
    
    public interface Department {
        public void selfAction();
        public void outAction();
    }
    public class Developement implements Department{
        private Mediator meidator;
        
        public Developement(Mediator meidator) {
            super();
            this.meidator = meidator;
            meidator.register("Development", this);
        }
    
        @Override
        public void selfAction() {
            // TODO Auto-generated method stub
            System.out.println("搞研发");
        }
    
        @Override
        public void outAction() {
            // TODO Auto-generated method stub
            System.out.println("汇报工作");
            meidator.commands("Development");
        }    
    }
    
    public class Finacial implements Department{
        private Mediator meidator;
        
        public Finacial(Mediator meidator) {
            super();
            this.meidator = meidator;
            meidator.register("Finacial", this);
        }
    
        @Override
        public void selfAction() {
            // TODO Auto-generated method stub
            System.out.println("数钱");
        }
    
        @Override
        public void outAction() {
            // TODO Auto-generated method stub
            System.out.println("汇报工作,没钱了");
            meidator.commands("Market");
        }
        
    }
    
    public class Market implements Department{
        private Mediator meidator;
        
        public Market(Mediator meidator) {
            super();
            this.meidator = meidator;
            meidator.register("Market", this);
        }
    
        @Override
        public void selfAction() {
            // TODO Auto-generated method stub
            System.out.println("市场调研");
        }
    
        @Override
        public void outAction() {
            // TODO Auto-generated method stub
            System.out.println("汇报工作,市场");
            meidator.commands("Finacial");
        }
    }
    public class Client {
        public static void main(String[] args) {
            Mediator mediator = new Persident();
            
            Market market = new Market(mediator);
            Finacial finacial = new Finacial(mediator);
            Developement developement = new Developement(mediator);
            
            market.selfAction();
            market.outAction();
            
        }
    }
    运行结果:

    市场调研
    汇报工作,市场
    数钱

    
    

    本质:解耦多个同事之间的交互关系,每个对象都持有中介对象的引用,只用跟中介者打交道。通过中介对象同一管理同事对象之间的交互关系。

    开发中常见场景:MVC模式,control层用来与M和V层进行交互,作为M和V的中介。

    GUI编程中,多个组件对象进行交互,可以引入一个中介对象来控制。

    java.lang.reflect.Method#invoke()方法

  • 相关阅读:
    我们需要测试!
    BlueStacks安装教程
    性能测试知多少系统架构分析
    免费去纽约 赢取总额20万美金 立即报名参加微软创新杯全球大学生大赛
    羡慕嫉妒别人只能说明自己不行
    算法和工程?那个重要......
    学习c++的优秀博客(更新ing)
    《启程》
    一点点反思
    提问的技巧
  • 原文地址:https://www.cnblogs.com/menbo/p/10176703.html
Copyright © 2011-2022 走看看