GOF23设计模式之 中介者模式Mediator

核心

  • 如果一个系统中对象之间的联系呈现为网状结构,对象之间存在大量多对多的关系,将熬制关系及其复杂,这些对象称之为 “同事对象”
  • 我们可以阴雨一个中介者对象,使各个同事对象只跟中介者打交道,将复杂的网络结构化解为星形结构。

中介者模式类图

GOF23设计模式之 中介者模式Mediator_第1张图片

中介者模式的本质

  • 解耦多个同事对象之间的交互关系。每个对象都持有中介者对象的引用,只跟中介者对象打交道。我们通过中介者对象统一管理这些交互关系

开发当中常见的场景

  • MVC模式(其中的C,控制器就是一个中介者对象。M和V都和他打交道)
  • 窗口游戏程序,窗口软件开发中窗口对象也是一个中介者对象
  • 图形界面开发GUI中,多个组件之间的交互,可以通过引入一个中介者对象来解决,可以使整体的窗口对象或者DOM对象
    -Java.lang.reflect.Method#invoke()

一种实现

  • Mediator
package com.coderbean.mediator;

/**
 * Created by Chang on 15/10/3.
 */
public interface Mediator {
    void register(String dname, Department d);
    void command(String name);
}
  • President
package com.coderbean.mediator;

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

/**
 * Created by Chang on 15/10/3.
 */
public class President implements Mediator {

    private Map map = new HashMap();

    @Override
    public void register(String dname, Department d) {
        this.map.put(dname,d);
    }

    @Override
    public void command(String name) {
        map.get(name).selfAction();
    }
}
  • Department
package com.coderbean.mediator;

/**
 * 同事类接口
 * Created by Chang on 15/10/3.
 */
public interface Department {
    void selfAction();  //做本部门的事情
    void outAction();   //向总经理发出申请
}
  • Finacial
package com.coderbean.mediator;

/**
 * Created by Chang on 15/10/3.
 */
public class Finacial implements Department {

    private Mediator m;

    public Finacial(Mediator m) {
        this.m = m;
        m.register("Finacial", this);
    }

    @Override
    public void selfAction() {
        System.out.println("数钱");
    }

    @Override
    public void outAction() {
        System.out.println("汇报工作,钱太多了,不知道怎么花?");
    }
}
  • Market
package com.coderbean.mediator;

/**
 * Created by Chang on 15/10/3.
 */
public class Market implements Department {

    private Mediator m;

    public Market(Mediator m) {
        this.m = m;
        m.register("Market", this);
    }

    @Override
    public void selfAction() {
        System.out.println("跑去接项目");
    }

    @Override
    public void outAction() {
        System.out.println("汇报工作,项目承接的进度,需要资金支持");
        m.command("Finacial");
    }
}
  • Development
package com.coderbean.mediator;

/**
 * Created by Chang on 15/10/3.
 */
public class Development implements Department {

    private Mediator m;

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

    @Override
    public void selfAction() {
        System.out.println("专心科研,做研发");
    }

    @Override
    public void outAction() {
        System.out.println("汇报工作,没钱了");
    }
}
  • Client
package com.coderbean.mediator;

/**
 * Created by Chang on 15/10/3.
 */
public class Client {
    public static void main(String[] args) {

        Mediator mediator = new President();
        Department market = new Market(mediator);
        Department department = new Development(mediator);
        Department Finacial = new Finacial(mediator);

        market.selfAction();
        market.outAction();
    }
}

你可能感兴趣的:(Java)