java 中的 Mediator 模式示例

介體模式定義了一個物件(Mediator),它封裝了一組物件的互動方式。它實現了多對多通訊。

UML 圖:

https://i.stack.imgur.com/sK7yu.gif

關鍵零件:

Mediator:定義同事之間的通訊介面。

Colleague:是一個抽象類,它定義了同事之間要傳達的事件

ConcreteMediator:通過協調 Colleague 物件實現合作行為並維護其同事

ConcreteColleague:實現通過 Mediator 收到的通知操作,這是由其他 Colleague 生成的

一個現實世界的例子:

你正在維護 Mesh 拓撲中的計算機網路。

網狀網路是一種網路拓撲,其中每個節點中繼網路的資料。所有網狀節點在網路中的資料分佈中協作。

如果新增了新計算機或刪除了現有計算機,則該網路中的所有其他計算機應該知道這兩個事件。

讓我們看看 Mediator 模式如何適應它。

程式碼段:

import java.util.List;
import java.util.ArrayList;

/* Define the contract for communication between Colleagues. 
   Implementation is left to ConcreteMediator */
interface Mediator{
    void register(Colleague colleague);
    void unregister(Colleague colleague);
}
/* Define the contract for notification events from Mediator. 
   Implementation is left to ConcreteColleague
*/
abstract class Colleague{
    private Mediator mediator;
    private String name;
    
    public Colleague(Mediator mediator,String name){
        this.mediator = mediator;
        this.name = name;
    }
    public String toString(){
        return name;
    }
    public abstract void receiveRegisterNotification(Colleague colleague);
    public abstract void receiveUnRegisterNotification(Colleague colleague);    
}
/*  Process notification event raised by other Colleague through Mediator.   
*/
class ComputerColleague extends Colleague {
    private Mediator mediator;
    
    public ComputerColleague(Mediator mediator,String name){
        super(mediator,name);
    }
    public  void receiveRegisterNotification(Colleague colleague){
        System.out.println("New Computer register event with name:"+colleague+
        ": received @"+this);
        // Send further messages to this new Colleague from now onwards
    }
    public  void receiveUnRegisterNotification(Colleague colleague){
        System.out.println("Computer left unregister event with name:"+colleague+
        ":received @"+this);
        // Do not send further messages to this Colleague from now onwards
    }
}
/* Act as a central hub for communication between different Colleagues. 
   Notifies all Concrete Colleagues on occurrence of an event
*/
class NetworkMediator implements Mediator{
    List<Colleague> colleagues = new ArrayList<Colleague>();
    
    public NetworkMediator(){
    
    }
    
    public void register(Colleague colleague){
        colleagues.add(colleague);
        for (Colleague other : colleagues){
            if ( other != colleague){
                other.receiveRegisterNotification(colleague);
            }
        }
    }
    public void unregister(Colleague colleague){
        colleagues.remove(colleague);
        for (Colleague other : colleagues){
            other.receiveUnRegisterNotification(colleague);
        }
    }
}

public class MediatorPatternDemo{
    public static void main(String args[]){
        Mediator mediator = new NetworkMediator();
        ComputerColleague colleague1 = new ComputerColleague(mediator,"Eagle");
        ComputerColleague colleague2 = new ComputerColleague(mediator,"Ostrich");
        ComputerColleague colleague3 = new ComputerColleague(mediator,"Penguin");
        mediator.register(colleague1);
        mediator.register(colleague2);
        mediator.register(colleague3);
        mediator.unregister(colleague1);
    }
}

輸出:

New Computer register event with name:Ostrich: received @Eagle
New Computer register event with name:Penguin: received @Eagle
New Computer register event with name:Penguin: received @Ostrich
Computer left unregister event with name:Eagle:received @Ostrich
Computer left unregister event with name:Eagle:received @Penguin

說明:

  1. Eagle 首先通過註冊事件新增到網路中。自 Eagle 成為第一個以來,沒有任何其他同事的通知。
  2. Ostrich 新增到網路時,會通知 Eagle:現在呈現輸出的第 1 行。
  3. Penguin 新增到網路時,EagleOstrich 都已通知:現在呈現輸出的第 2 行和第 3 行。
  4. Eagle 通過註冊事件離開網路時,OstrichPenguin 都已被通知。現在渲染輸出的第 4 行和第 5 行。