Mediator中介者模式属于行为型设计模式,中介者模式定义了一个中介对象来封装一系列对象之间的交互关系。中介者使各个对象之间不需要显式地相互引用,从而使耦合性降低,而且可以独立地改变它们之间的交互行为。
Design Pattern:Mediator 🔗
基本介绍 🔗
构成 🔗
- Mediator:抽象中介者类,定义各个Colleague之间交互接口。
- ConcreteMediator:具体中介者类,持有各个ConcreteColleague对象的引用,并负责具体的协调ConcreteColleague对象之间的交互关系。
- Colleague:抽象同事类,持有Mediator对象的引用,并且定义ConcreteColleague之间的公共功能。
- ConcreteColleague:具体同事类,通过持有的中介者与各个ConcreteColleague进行通信,并且实现公共功能。
优点 🔗
- 中介者模式通过把多个同事对象之间的交互封装到中介对象里面,从而使得对象之间松散耦合,基本上可以做到互不依赖。
- 多个同事对象的交互,被封装在中介者对象里面集中管理,实现集中控制交互。
- 同事之间的对应关系由多对多变成了一对多。
缺点 🔗
- 中介者对象的大小和复杂性很容易增加,变得难以管理。
实例 🔗
C#中介者模式的实例,当了一段时间董事长,我发现还是喜欢开发游戏,所以我有钱的老爸给我投资了一个游戏工作室。
/// <summary>
/// 抽象中介者类
/// </summary>
public abstract class Mediator
{
public abstract void Command(Colleague colleague);
}
/// <summary>
/// 具体中介者类 - 项目经理
/// </summary>
public class ProductionManagerMediator : Mediator
{
private PlanningColleague _planning;
private EngineerColleague _engineer;
private TestingColleague _testing;
public void SetPlanningColleague(PlanningColleague planning)
{
_planning = planning;
}
public void SetPlanningColleague(EngineerColleague engineer)
{
_engineer = engineer;
}
public void SetPlanningColleague(TestingColleague testing)
{
_testing = testing;
}
public override void Command(Colleague colleague)
{
if (colleague.GetType() == typeof(PlanningColleague))
{
_engineer.Apply();
}
else if (colleague.GetType() == typeof(EngineerColleague))
{
_testing.Apply();
}
else if (colleague.GetType() == typeof(TestingColleague))
{
_planning.Apply();
}
}
}
/// <summary>
/// 抽象同事类
/// </summary>
public abstract class Colleague
{
protected Mediator _mediator;
public Colleague(Mediator mediator)
{
_mediator = mediator;
}
/// <summary>
/// 发送消息
/// </summary>
public abstract void Send();
/// <summary>
/// 执行
/// </summary>
public abstract void Apply();
}
/// <summary>
/// 具体同事类 - 策划同事
/// </summary>
public class PlanningColleague : Colleague
{
public PlanningColleague(Mediator mediator) : base(mediator)
{
}
public override void Send()
{
_mediator.Command(this);
}
public override void Apply()
{
Console.WriteLine("我是策划,产品验收完毕。");
}
}
/// <summary>
/// 具体同事类 - 技术同事
/// </summary>
public class EngineerColleague : Colleague
{
public EngineerColleague(Mediator mediator) : base(mediator)
{
}
public override void Send()
{
_mediator.Command(this);
}
public override void Apply()
{
Console.WriteLine("我是程序,收到策划案,开始研发工作。");
}
}
/// <summary>
/// 具体同事类 - 测试同事
/// </summary>
public class TestingColleague : Colleague
{
public TestingColleague(Mediator mediator) : base(mediator)
{
}
public override void Send()
{
_mediator.Command(this);
}
public override void Apply()
{
Console.WriteLine("我是测试,收到产品,开始测试工作。");
}
}
实际运行:
ProductionManagerMediator productionManager = new ProductionManagerMediator();
PlanningColleague planning = new PlanningColleague(productionManager);
EngineerColleague engineer = new EngineerColleague(productionManager);
TestingColleague testing = new TestingColleague(productionManager);
planning.Send();
engineer.Send();
testing.Send();
结论 🔗
搬砖愉快!