动机(Motivation)
在软件构建的过程中,经常会出现多个对象互相关联交互的情况,对象之间常常会维护一种复杂的引用关系,如果遇到一些需求的更改,这种直接的引用关系将面临不断的变化。
在这种情况下,我们可使用一个“中介对象”来管理对象间的关联关系,避免相互交互的对象之间的紧耦合引用关系,从而更好地抵御变化。
意图(Intent)
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
——《设计模式》GoF
示例代码
//不使用中介者时的交互
class CutMenuItem
{
TextArea textArea;
ClipBoard clipBoard;
ToolBarButton toolBarButton;
public void Click()
{
string text = textArea.SelectedText();
textArea.RemoveSelectionText();
clipBoard.SetData(text);
ToolBarButton.EnabledPasteButton(true);
}
}
class TextArea
{
ClipBoard clipBoard;
ToolBarButton toolBarButton;
CutMenuItem cutMenuItem;
public void Process()
{
//...
}
}
class ClipBoard
{
TextArea textArea;
CutMenuItem menuItem;
ToolBarButton toolBarButton;
//...
}
class ToolBarButton
{
//也要有其它几个对象的引用;
}
//加入中介者时的代码
abstract class Mediator
{
public abstract void Notify();
public abstract void AddElement(Elment element)
{
list.Add(element);
}
ArrayList<Element> list = new ArrayList<Element>();
}
abstract class Element
{
public Element(Mediator mediator)
{
this.mediator = mediator;
this.mediator.AddElement(this);
}
public virtual void OnChange()
{
mediator.Notify();
}
}
class CutMenuItem:Element
{
public CutMenuItem(Mediator mediator)
:base(mediator)
{
//...
}
public void Click()
{
OnChange();
}
}
class TextArea:Element
{
public TextArea(Mediator mediator)
:base(mediator)
{
//...
}
public void Process()
{
OnChange();
}
}
//其它几个对象同样处理
public class ConcreteMediator:Mediator
{
public override void Notify()
{
//...
}
}
//Mediator和Element互相依赖,但其它对象之间不再直接相互依赖了
Mediator模式的几个要点
1)将多个对象间复杂的关联关系解耦,Mediator模式将多个对象间的控制逻辑进行集中管理,变“多个对象互相关联”为“多个对象和一个中介者关联”,简化了系统的维护,抵御了可能的变化。
2)随着控制逻辑的复杂化,Mediator具体对象的实现可能相当复杂,这时可对Mediator对象进行分解处理。
3)Facade模式是解耦系统外到系统内(单向)的对象关联关系;Mediator模式是解耦系统内各个对象之间(双向)的关联关系。