欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > C# 中介者模式

C# 中介者模式

2024/10/24 20:21:06 来源:https://blog.csdn.net/m0_46396765/article/details/140636905  浏览:    关键词:C# 中介者模式

栏目总目录


概念

中介者模式(Mediator Pattern)是一种行为设计模式,它允许一组对象相互通信,而不需要知道彼此的内部结构。通过引入一个中介者对象,将原本对象之间的直接交互转化为与中介者对象的交互,从而降低了对象之间的耦合度,使得系统更加灵活和易于维护。这种模式特别适用于对象之间存在大量复杂交互关系的场景。

角色

在中介者模式中,主要存在以下几个角色:

  1. 抽象中介者(Mediator):定义了一个接口,用于与各个同事(Colleague)对象通信。它通常包含一个或多个用于处理同事对象之间通信的方法。

  2. 具体中介者(ConcreteMediator):实现了抽象中介者接口,并协调各个同事对象之间的交互。它知道所有的同事对象,并控制它们之间的通信。

  3. 抽象同事类(Colleague):定义了具体对象的接口,这些对象知道自己的中介者对象,并与其通信,而不是直接与其他同事对象通信。

  4. 具体同事类(ConcreteColleague):实现了抽象同事类,并定义了自己的业务逻辑。在需要与其他同事通信时,通过中介者对象进行。

好处

  1. 降低耦合度:通过引入中介者对象,降低了对象之间的直接依赖,使得系统更加灵活。
  2. 易于扩展和维护:当需要增加新的同事对象或修改现有对象之间的交互时,只需修改中介者对象即可,无需修改每个同事对象。
  3. 集中控制:所有对象之间的交互都通过中介者对象进行,使得交互规则更加清晰明确,易于理解和修改。
  4. 提高系统性能:中介者对象可以对对象之间的交互进行统一管理和优化,从而提高系统的性能和效率。

应用场景

  1. 复杂的GUI系统:在图形用户界面(GUI)中,不同的控件之间需要进行复杂的交互,这时可以使用中介者模式来管理这些交互。
  2. 网络通信:在网络通信中,不同的设备或组件之间通过服务器作为中介对象进行交互,这也是中介者模式的一个典型应用。
  3. MVC框架:在MVC(Model-View-Controller)框架中,控制器(Controller)充当了模型(Model)和视图(View)之间的中介者,负责协调它们之间的数据流动。

示例代码

以下是使用C#实现的中介者模式的一个简单示例:

// 抽象中介者
public abstract class Mediator
{public abstract void Send(string message, Colleague colleague);
}// 具体中介者
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);}}
}// 抽象同事类
public abstract class Colleague
{protected Mediator mediator;public Colleague(Mediator mediator){this.mediator = mediator;}public abstract void Send(string message);public abstract void Notify(string message);
}// 具体同事类1
public class ConcreteColleague1 : Colleague
{public ConcreteColleague1(Mediator mediator) : base(mediator){}public override void Send(string message){mediator.Send(message, this);}public override void Notify(string message){Console.WriteLine("Colleague1 gets message: " + message);}
}// 具体同事类2
public class ConcreteColleague2 : Colleague
{public ConcreteColleague2(Mediator mediator) : base(mediator){}public override void Send(string message){mediator.Send(message, this);}public override void Notify(string message){Console.WriteLine("Colleague2 gets message: " + message);}
}// 客户端代码class Program{static void Main(string[] args){// 创建中介者var mediator = new ConcreteMediator();// 创建同事对象,并将它们与中介者关联var colleague1 = new ConcreteColleague1(mediator);var colleague2 = new ConcreteColleague2(mediator);// 设置中介者中的同事对象引用mediator.Colleague1 = colleague1;mediator.Colleague2 = colleague2;// 同事对象通过中介者发送消息colleague1.Send("Hello from Colleague1!");colleague2.Send("Greetings from Colleague2!");// 等待用户输入,避免程序直接退出Console.ReadKey();}}
}

在这个示例中,我们定义了一个抽象的中介者Mediator和一个具体的中介者ConcreteMediator,以及两个具体的同事类ConcreteColleague1ConcreteColleague2。每个同事类都持有一个中介者的引用,并通过中介者来发送和接收消息。

Main方法中,我们创建了一个中介者实例和两个同事对象实例,并将它们相互关联。然后,我们让两个同事对象通过中介者发送消息。由于中介者知道所有同事对象,并控制它们之间的交互,因此消息能够正确地传递给接收者。

通过运行这个示例,你将看到控制台输出类似以下信息:

Colleague2 gets message: Hello from Colleague1!
Colleague1 gets message: Greetings from Colleague2!

这表明Colleague1发送的消息被Colleague2接收,而Colleague2发送的消息被Colleague1接收,这正是通过中介者模式实现的。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com