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 行。