美文网首页
设计模式-行为篇(中介者模式)

设计模式-行为篇(中介者模式)

作者: 小酷哥 | 来源:发表于2017-02-10 14:19 被阅读0次

中介者模式

mvc的模型,Meditor控制

/**
 * Created by malei on 2016/12/6.
 * 中介者接口
 */
public interface Mediator {
    void listener(String key,Department de);
    void say(String key);
}

/**
 * Created by malei on 2016/12/6.
 * 部门接口
 */
public interface Department {
    void action();
}

/**
 * Created by malei on 2016/12/6.
 * 研发部
 */
public class Delelopment implements Department {

    private final Mediator mediator;

    public Delelopment(Mediator mediator){
        this.mediator = mediator;
    }

    @Override
    public void action() {
        Log.show("研发部活动");
    }
}

/**
 * Created by malei on 2016/12/6.
 * 市场部
 */
public class Market implements Department {

    private final Mediator mediator;

    public Market(Mediator mediator){
        this.mediator = mediator;
    }

    @Override
    public void action() {
        Log.show("市场部活动");
    }

    public static void main(String[] args){
        Mediator mediator = new CEO();
        Market market = new Market(mediator);
        Delelopment dev = new Delelopment(mediator);

        market.action();
        dev.action();
    }
}

/**
 * Created by malei on 2016/12/6.
 * ceo:中介者的具体实现类
 */
public class CEO implements Mediator{

    private Map<String, Department> map = new HashMap<String, Department>();

    @Override
    public void listener(String key, Department de) {
        map.put(key, de);
    }

    @Override
    public void say(String key) {
        map.get(key).action();
    }
}


相关文章

网友评论

      本文标题:设计模式-行为篇(中介者模式)

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