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接收,这正是通过中介者模式实现的。

  • 24
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

語衣

感谢大哥

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值