Mediator中介者模式
处理一个剪贴板之类的部分控件模块之间的关系,如选定一部分文本按撤消按钮或者按菜单兰里面的部分功能,他们之间都是相关的,如下有两种做法:
传统做法
class CutMenuItem
{
TextArea textArea;
ClickBoard clickBoard;
ToolBarButton toolBarButton;
public void Click()
{
string text = textArea.SelectedText();
textArea.RemoveSelectionText();
clickBoard.SetData(text);
toolBarButton.EnabledPasteButton(true);
}
}
class TextArea
{
ClipBoard clipBoard;
ToolBarButton toolBarButton;
CutMenuItem cutMenuItem;
public void Process()
{
}
}
class ClipBorad
{
ToolBarButton toolBarButton;
CutMenuItem cutMenuItem;
TextArea textArea;
}
class ToolBarButton
{
CutMenuItem cutMenuItem;
TextArea textArea;
ClipBoard clipBoard;
}
运用中介者设计模式:
class CutMenuItem
{
TextArea textArea;
ClickBoard clickBoard;
ToolBarButton toolBarButton;
public void Click()
{
string text = textArea.SelectedText();
textArea.RemoveSelectionText();
clickBoard.SetData(text);
toolBarButton.EnabledPasteButton(true);
}
}
class TextArea
{
ClipBoard clipBoard;
ToolBarButton toolBarButton;
CutMenuItem cutMenuItem;
public void Process()
{
}
}
class ClipBorad
{
ToolBarButton toolBarButton;
CutMenuItem cutMenuItem;
TextArea textArea;
}
class ToolBarButton
{
CutMenuItem cutMenuItem;
TextArea textArea;
ClipBoard clipBoard;
}
中介者模式的做法
using System.Collections;
abstract class Mediator
{
public abstract void Notify();
public abstract void AddElement(Element element)
{
list.Add(element);
}
ArrayList<Element> list = new ArrayList<Element>();
}
abstract class Element
{
Mediator mediator;
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()
{
mediator.Notify();
}
}
class TextArea : Element
{
public TextArea(Mediator mediator) : base(mediator)
{
//
}
public void Process()
{
OnChange();
}
private void OnChange()
{
mediator.Notify();
}
}
class ClipBorad : Element
{
public ClipBorad(Mediator mediator)
: base(mediator)
{
//
}
public void Invoke()
{
OnChange();
}
}
class ToolBarButton : Element
{
public ToolBarButton(Mediator mediator)
: base(mediator)
{
//.
}
public void Click()
{
onChange();
}
}
/**//// <summary>
/// *******8
/// </summary>
class ConcreateMediator : Mediator
{
public override void Notify()
{
}
}
using System.Collections;
abstract class Mediator
{
public abstract void Notify();
public abstract void AddElement(Element element)
{
list.Add(element);
}
ArrayList<Element> list = new ArrayList<Element>();
}
abstract class Element
{
Mediator mediator;
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()
{
mediator.Notify();
}
}
class TextArea : Element
{
public TextArea(Mediator mediator) : base(mediator)
{
//
}
public void Process()
{
OnChange();
}
private void OnChange()
{
mediator.Notify();
}
}
class ClipBorad : Element
{
public ClipBorad(Mediator mediator)
: base(mediator)
{
//
}
public void Invoke()
{
OnChange();
}
}
class ToolBarButton : Element
{
public ToolBarButton(Mediator mediator)
: base(mediator)
{
//.
}
public void Click()
{
onChange();
}
}
/**//// <summary>
/// *******8
/// </summary>
class ConcreateMediator : Mediator
{
public override void Notify()
{
}
}