zoukankan      html  css  js  c++  java
  • 设计模式之中介者模式 Mediator

    代码实现

    public interface Mediator {
    
        void register(String dname,Department d);
        void command(String dname);
    }
    中介者接口
    /*
     * 同事类接口
     */
    public interface Department {
    
        void selfAction(); //做本部门工作
        void outAction();  //向总经理发出申请
    }
    同事类接口
    public class Development implements Department{
        
        private Mediator m ; //持有中介者(总经理)的引用
    
        public Development(Mediator m) {
            super();
            this.m = m;
            m.register("development", this);
        }
    
        @Override
        public void selfAction() {
            System.out.println("开发项目");
        }
    
        @Override
        public void outAction() {
            System.out.println("汇报工作,需要资金支持");
        }
    
    }
    
    
    public class Finacial implements Department{
        
        private Mediator m ; //持有中介者(总经理)的引用
    
        public Finacial(Mediator m) {
            super();
            this.m = m;
            m.register("finacial", this);
        }
    
        @Override
        public void selfAction() {
            System.out.println("数钱");
        }
    
        @Override
        public void outAction() {
            System.out.println("汇报工作,钱怎么分配");
        }
    
    }
    
    
    public class Market implements Department{
        
        private Mediator m ; //持有中介者(总经理)的引用
    
        public Market(Mediator m) {
            super();
            this.m = m;
            m.register("market", this);
        }
    
        @Override
        public void selfAction() {
            System.out.println("接项目");
        }
    
        @Override
        public void outAction() {
            System.out.println("汇报工作,最新市场动态,需要资金支持");
            m.command("finacial");
        }
    
    }
    同事类实现
    public class President implements Mediator{
    
        private Map<String,Department> map = new HashMap<String,Department>();
        
        @Override
        public void register(String dname, Department d) {
            map.put(dname,d);
        }
    
        @Override
        public void command(String dname) {
            map.get(dname).selfAction();
        }
    
    }
    中介者实现
    public class Client {
    
        public static void main(String[] args) {
            Mediator m = new President();
            
            Market market = new Market(m);
            Development devp = new Development(m);
            Finacial f = new Finacial(m);
            
            market.selfAction();
            market.outAction();
        }
    }
    测试
  • 相关阅读:
    JVM收藏的文章
    【转】Mysql相关子查询&&MySQL获取分组后的TOP N记录
    【转】JVM--内存区域划分
    【转】Nginx location写法
    【转】Dockerfile
    CORS web.xml 里配置
    分布式事务
    maven+dubbo+SpringMVC 项目搭建
    dubbo 报错
    多重背包问题:POJ2392
  • 原文地址:https://www.cnblogs.com/qingdaofu/p/7478000.html
Copyright © 2011-2022 走看看