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()方法

  • 相关阅读:
    主机无法ping通网关,但可以ping通局域网内的其他主机
    linux下安装mysql
    国内python源
    在vue中引入element-ui时报错
    在windows上部署vue
    linux上部署vue开发环境
    vue插件之vue-router路由基本使用
    vue监听属性变化
    vue过滤器
    vue组件的使用
  • 原文地址:https://www.cnblogs.com/menbo/p/10176703.html
Copyright © 2011-2022 走看看