中介者模式:用一个中介者对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
UML结构图:
代码:
//抽象中介者类
public abstract class Mediator
{
public abstract void Send(string message,Colleague colleague);
}
//抽象同事者类
public abstract class Colleague
{
protected Mediator mediator;
public Colleague(Mediator mediator)
{
this.mediator = mediator;
}
}
//具体中介类
public class ConcreteMediator : Mediator
{
private ConcreteColleague1 colleague1;
private ConcreteColleague2 colleague2;
public ConcreteColleague1 Colleague1
{
set { colleague1 = value; }
}
public ConcreteColleague2 Colleague2
{
set { colleague2 = value; }
}
public override void Send(string message, Colleague colleague)
{
if (colleague == colleague1)
{
colleague2.Notify(message);
}
else
{
colleague1.Notify(message);
}
}
}
//具体同事者类1
public class ConcreteColleague1 : Colleague
{
public ConcreteColleague1(Mediator mediator) : base(mediator) { }
public void Send(string message)
{
mediator.Send(message,this);
}
public void Notify(string message)
{
Console.WriteLine("同事1得到信息:"+message);
}
}
//具体同事者类2
public class ConcreteColleague2 : Colleague
{
public ConcreteColleague2(Mediator mediator) : base(mediator) { }
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine("同事2得到信息:"+message);
}
}
客户端代码:
ConcreteMediator m = new ConcreteMediator();
ConcreteColleague1 c1 = new ConcreteColleague1(m);
ConcreteColleague2 c2 = new ConcreteColleague2(m);
m.Colleague1 = c1;
m.Colleague2 = c2;
c1.Send("吃过饭了嘛");
c2.Send("没有呢,你打算请客?");
显示:
下面听题:
联合国处理美国和伊拉克的问题。
//联合国
public abstract class UnitedNations
{
public abstract void Declare(string message,Country country);
}
//国家
public abstract class Country
{
protected UnitedNations mediator;
public Country(UnitedNations mediator)
{
this.mediator = mediator;
}
}
//美国
public class USA : Country
{
public USA(UnitedNations mediator) : base(mediator) { }
public void Declare(string message)
{
mediator.Declare(message,this);
}
public void Notify(string message)
{
Console.WriteLine("美国收到消息"+message);
}
}
//伊拉克
public class Iraq : Country
{
public Iraq(UnitedNations mediator) : base(mediator) { }
public void Declare(string message)
{
mediator.Declare(message,this);
}
public void Notify(string message)
{
Console.WriteLine("伊拉克收到消息"+message);
}
}
//联合国安理会
public class UnitedNationsSecuritycouncil : UnitedNations
{
private USA usa;
private Iraq iraq;
public USA USA
{
set { usa = value; }
}
public Iraq Iraq
{
set { iraq = value; }
}
public override void Declare(string message, Country country)
{
if (country == usa)
{
iraq.Notify(message);
}
else
{
usa.Notify(message);
}
}
}
```
客户端代码:
UnitedNationsSecuritycouncil UNSC = new UnitedNationsSecuritycouncil();
USA usa = new USA(UNSC);
Iraq iraq = new Iraq(UNSC);
UNSC.USA = usa;
UNSC.Iraq = iraq;
usa.Declare("不准研制核武器,否则发动战争!");
iraq.Declare("我们没有核武器,也不怕侵略");
“`
结果:
总结:
优点:
1)将各个对象类解耦,同时简化对象之间的关系,使系统变为松耦合。
2)系统更加灵活,各个对象独立而易于复用。
缺点:
1)中介者模式中,中介者承担了较大的责任,一旦中介者出现问题,整个系统都会受到较大的影响。
2)新增一个对象类,需要修改抽象中介者和具体中介者类。
适用场景:
中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通信的场合,及想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。