java23中设计模式之中介者模式

package com.bdqn.mediator;
/**
 * 部门接口
 * @author OU
 *
 */
public interface Department {
    void selfAction();//做本部门的事情
    void outAction();//向总经理发出申请
    
}
department
package com.bdqn.mediator;
/**
 * 研发部门
 * @author OU
 *
 */
public class Development implements Department{
     private Mediator m;//持有中介者引用
     
    public Development() {
    }

    public Development(Mediator m) {
        super();
        this.m = m;
        m.register("development", this);
    }

    public void selfAction() {
        System.out.println("汇报工作!没钱了,需要资金支持");
        
    }

    public void outAction() {
    System.out.println("专心科研,开发项目");        
    }
     
}
development
package com.bdqn.mediator;
/**
 * 财务部
 * @author OU
 *
 */
public class Finacial implements Department {
    private Mediator m;
    
    public Finacial(Mediator m) {
        super();
        this.m = m;
        m.register("finacial", this);
    }

    public void selfAction() {
        System.out.println("数钱");
    }
    public void outAction() {
        System.out.println("汇报工作!没钱了,钱太多了!怎么花");
    }
  
}
finacial
package com.bdqn.mediator;
/**
 * 市场部
 * @author OU
 *
 */
public class Market implements Department {
     private Mediator m;
        
        public Market(Mediator m) {
            super();
            this.m = m;
            m.register("market", this);
        }
        public void outAction() {
            System.out.println("汇报工作,项目承接的进度,需要资金支持");
            m.command("finacial");
        }

    public void selfAction() {
        System.out.println("跑去接项目");
    }
}
market
package com.bdqn.mediator;
/**
 * 中介者
 * @author OU
 *
 */
public interface Mediator {
   void register(String dname,Department d);
   void command(String dname);
    
}
mediator
package com.bdqn.mediator;

import java.util.HashMap;
import java.util.Map;

/**
 * 总经理
 * @author OU
 *
 */
public class President implements Mediator {
    
    private Map< String, Department> map=new HashMap<String, Department>();
    
    public void register(String dname, Department d) {
        map.put(dname, d);
    }

    public void command(String dname) {
        map.get(dname).selfAction();
    }

}
president
package com.bdqn.mediator;

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();
}
}
client
原文地址:https://www.cnblogs.com/ou-pc/p/7518005.html