中介者(Mediator)

意图

用一个中介对象封装一系列的对象交互,中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

结构

中介者(Mediator)_第1张图片
中介者结构图

动机

面向对象设计鼓励将行为分布到各个对象中,以提高这些行为的可复用性。但是,采用这种分割方式的系统,也可能会引起对象之间的相互连接(直接引用)激增,反而降低了它们的可复用性(依赖与耦合过高)。

中介者模式通过将集体行为封装到一个独立的中介者对象,由中介者负责控制和协调对象间的交互,使对象间不再相互显式引用,提高可复用性。

适用性

  • 一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解;
  • 一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象;
  • 想定制一个分布在多个类中的行为,而又不想生成太多的子类。

优缺点

  • 中介者把各个对象组织在一起,负责协调它们之间的业务逻辑。如果业务发生变化,只需要生成Mediator子类即可,这样各个Colleague类可被重用;
  • 中介者有利于各个Colleague之间的松耦合,可以独立地改变和复用Mediator及Colleague;
  • 中介者抽象了对象间的协作行为,使关注点从对象本身的行为转移到对象间的交互。有助于我们弄清楚一个系统中的对象是如何交互的。
  • 中介者的集中化控制的特点,使得交互的复杂性变成了中介者的复杂性。

其他

  • Mediator 与 Colleague 之间的通讯方式:
    1. 使用观察者模式:Colleague类为Subject,Mediator类为Observer;
    2. 在Mediator中定义一个特殊的通知接口,各Colleague在通知时直接调用该接口。


示例

模拟一个图形用户界面,该界面有2个窗体控件:下拉列表框(ListBox)和文本框(TextBox)。当下拉列表框某一项被选中时,文本框的内容将自动更新。

实现(C#)

中介者(Mediator)_第2张图片
图形界面结构图

以及它们之间的交互时序图:

中介者(Mediator)_第3张图片
图形用户界面交互时序图

using System;

public abstract class Mediator
{
    public abstract void CreateWidgets();
    public abstract void WidgetChanged(Widget widget);
}

public class DialogMediator : Mediator
{
    private ListBox list;
    private TextBox field;

    public override void CreateWidgets()
    {
        list = new ListBox(this);
        field = new TextBox(this);
    }

    public override void WidgetChanged(Widget widget)
    {
        if(widget is ListBox)
        {
            string text = list.GetSelectedItem();
            field.SetText(text);
        }
        // else if(widget is TextBox) ...
    }

    public ListBox ListBox { get { return this.list; }}
    public TextBox Field { get { return this.field; }}
    
}

// 图形窗体的基类
public abstract class Widget
{
    private readonly Mediator mediator;

    protected Widget(Mediator mediator)
    {
        this.mediator = mediator;
    }

    public void Changed()
    {
        this.mediator.WidgetChanged(this);
    }
}

// 列表框控件
public class ListBox : Widget
{
    private readonly string[] items;
    private int selectedIndex = -1;

    public ListBox(Mediator mediator) : base(mediator)
    {
        items = new [] {"1.华为", "2.小米", "3.OPPO", "4.三星", "5.苹果"};
    }

    public void SetSelectedItem(int index)
    {
        this.selectedIndex = index;

        // 引发一个事件。
        this.Changed();
    }

    public string GetSelectedItem()
    {
        if(this.selectedIndex >= 0 && this.selectedIndex < items.Length)
        {
            return items[this.selectedIndex];
        }
        else
        {
            return "未选中";
        }
    }

}

// 文本框控件
public class TextBox : Widget
{
    private string text;

    public TextBox(Mediator mediator) : base(mediator) {}

    public void SetText(string text)
    {
        this.text = text;
    }

    public string GetText()
    {
        return this.text;
    }
}

// 测试
public class App
{
    public static void Main(string[] args)
    {
        DialogMediator dialog = new DialogMediator();

        //1. 创建各个控件
        dialog.CreateWidgets();

        //2.模拟选中某个列表项动作
        dialog.ListBox.SetSelectedItem(2);
        Console.WriteLine(dialog.Field.GetText());

        //2.模拟选中某个列表项动作
        dialog.ListBox.SetSelectedItem(4);
        Console.WriteLine(dialog.Field.GetText());
    }
}

// 控制台输出:
//   3.OPPO
//   5.苹果


你可能感兴趣的:(中介者(Mediator))