JAVA設計模式之中介模式

xyw55發表於2016-12-17

JAVA設計模式之中介模式

概念:

中介模式(mediator):如果一個系統中物件之間是網狀結構的,多對多的關係,會導致系統極其複雜,此時可以引入一箇中介者物件,負責各個物件的協調,即各個物件只跟中介者打交道,這樣負責網路結構就轉化為星狀結構。
中介者模式

類圖

中介者模式類圖

應用

  1. MVC模式中的C,控制器就是一箇中介者物件。M和V都和他打交道。
  2. 視窗遊戲程式,視窗軟體開發中視窗也是一箇中介者物件。
  3. 影象介面開發GUI,對個元件之間的互動,可以通過引入一箇中介者物件來解決,可以是整體的視窗物件或者DOM物件
  4. java.lang.reflect.Method#invoke()De

程式碼

中介者模式舉例類圖

Mediator介面:

public interface Mediator {
    void register(String name, Department department);

    void commond(String departmentName);
}

具體中介者:

public class President implements Mediator {
    private Map<String, Department> departmentMap = new HashMap<>();

    @Override
    public void register(String name, Department department) {
        this.departmentMap.put(name, department);
    }

    @Override
    public void commond(String departmentName) {
        Department d = this.departmentMap.get(departmentName);
        d.outAction();
    }
}

部門介面:

public interface Department {
    void selfAction();

    void outAction();
}

具體部門:

public class Development implements Department {
    private Mediator mediator;
    private String name;

    public Development(Mediator mediator, String name) {
        this.mediator = mediator;
        this.name = name;
        mediator.register(name, this);
    }

    @Override
    public void selfAction() {
        System.out.println(name + " coding");
    }

    @Override
    public void outAction() {
        System.out.println(name + " server is over, need new server");
        mediator.commond("financial");
    }
}
public class Financial implements Department {
    private Mediator mediator;
    private String name;

    public Financial(Mediator mediator, String name) {
        this.mediator = mediator;
        this.name = name;
        mediator.register(name, this);
    }

    @Override
    public void selfAction() {
        System.out.println(name + " manage money. get money");
    }

    @Override
    public void outAction() {
        System.out.println(name + " send monney");
    }
}
public class Market implements Department {
    private Mediator mediator;
    private String name;

    public Market(Mediator mediator, String name) {
        this.mediator = mediator;
        this.name = name;
        mediator.register(name, this);
    }

    @Override
    public void selfAction() {
        System.out.println(name + "sold production.");
    }

    @Override
    public void outAction() {
        System.out.println(name + "plan a activity, need money");
        mediator.commond("financial");
    }
}

客戶端呼叫:

public class Main {
    public static void main(String[] args) {
        President president = new President();
        Development development = new Development(president, "development");
        Financial financial = new Financial(president, "financial");
        Market market = new Market(president, "market");

        development.selfAction();
        development.outAction();

    }
}

總結

中介者模式的本質是解耦各個同時物件之間的互動關係。每個物件都持有中介者物件的引用,只跟中介者物件打交道。我們通過中介者物件同意管理這些互動關係。

相關文章