左边视图对应的控制对象为DirectorySelectorController,而右边视图对应的则为FileSelectorController对 象。Marlon统一定义了一个接口IColleague,作为Mediator模式中参与者的抽象接口,并让相关的Controller实现它。类图如下所示:

每个Controller对象所接收的Mediator对象都是相同的,因为Mediator对象作为BaseController基类的属性存在,并利用了Singleton模式,保证了Mediator对象只能存在一个:
在子类的构造函数中,通过调用Mediator对象的Register方法,建立了消息与Controller对象之间的映射关系。以FileSelectorController类为例:
public abstract class BaseController : INotifyPropertyChanged, IColleague
{
static Mediator mediatorInstance = new Mediator();
public Mediator Mediator { get; private set; }
public BaseController()
{
//set the mediator to be the same one for every controller.
Mediator = mediatorInstance;
}
//rest of implementation
}
{
static Mediator mediatorInstance = new Mediator();
public Mediator Mediator { get; private set; }
public BaseController()
{
//set the mediator to be the same one for every controller.
Mediator = mediatorInstance;
}
//rest of implementation
}
在子类的构造函数中,通过调用Mediator对象的Register方法,建立了消息与Controller对象之间的映射关系。以FileSelectorController类为例:
public FileSelectorController()
{
Mediator.Register(this, new[]
{
Messages.DirectorySelectedChanged
});
}
{
Mediator.Register(this, new[]
{
Messages.DirectorySelectedChanged
});
}