设计模式实践-中介者模式

场景

不同设备对象间通信

实现代码

中介者接口

namespace DesignPatterns.Mediator
{
    /// <summary>
    /// 中介者接口
    /// </summary>
    public interface IMediator
    {
        /// <summary>
        /// 向设备发送消息
        /// </summary>
        /// <param name="message">消息体</param>
        /// <param name="device">设备对象</param>
        void Send(string message, IDevice device);
    }
}

中介者实现

namespace DesignPatterns.Mediator
{
    /// <summary>
    /// 中介者
    /// </summary>
    public class Mediator : IMediator
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Mediator" /> class.
        /// </summary>
        public Mediator()
        {
        }

        /// <summary>
        /// Gets or sets 设备A对象
        /// </summary>
        public DeviceA DeviceA { get; set; }

        /// <summary>
        /// Gets or sets 设备B对象
        /// </summary>
        public DeviceB DeviceB { get; set; }

        /// <summary>
        /// 向设备发送数据
        /// </summary>
        /// <param name="message">消息体</param>
        /// <param name="device">设备对象</param>
        public void Send(string message, IDevice device)
        {
            if (device == this.DeviceA)
            {
                this.DeviceB.Notify(message);
            }
            else
            {
                this.DeviceA.Notify(message);
            }
        }
    }
}

设备接口

namespace DesignPatterns.Mediator
{
    /// <summary>
    /// 设备接口
    /// </summary>
    public interface IDevice
    {
        /// <summary>
        /// 接受消息
        /// </summary>
        /// <param name="message">消息体</param>
        void Notify(string message);

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message">消息体</param>
        void Send(string message);
    }
}

设备A实现

namespace DesignPatterns.Mediator
{
    /// <summary>
    /// 设备A对象
    /// </summary>
    public class DeviceA : IDevice
    {
        /// <summary>
        /// 中介者对象
        /// </summary>
        private Mediator _mediator;

        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceA" /> class.
        /// </summary>
        /// <param name="mediator">中介者对象</param>
        public DeviceA(Mediator mediator)
        {
            this._mediator = mediator;
        }

        /// <summary>
        /// 接收消息提示
        /// </summary>
        /// <param name="message">消息</param>
        public void Notify(string message)
        {
            Console.WriteLine($"Get message in A: {message}");
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message">消息体</param>
        public void Send(string message)
        {
            this._mediator.Send(message, this);
        }
    }
}

设备B实现

namespace DesignPatterns.Mediator
{
    /// <summary>
    /// 设备A对象
    /// </summary>
    public class DeviceB : IDevice
    {
        /// <summary>
        /// 中介者对象
        /// </summary>
        private Mediator _mediator;

        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceB" /> class.
        /// </summary>
        /// <param name="mediator">中介者对象</param>
        public DeviceB(Mediator mediator)
        {
            this._mediator = mediator;
        }

        /// <summary>
        /// 接收消息提示
        /// </summary>
        /// <param name="message">消息</param>
        public void Notify(string message)
        {
            Console.WriteLine($"Get message in B: {message}");
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message">消息体</param>
        public void Send(string message)
        {
            this._mediator.Send(message, this);
        }
    }
}

相关调用

Mediator.Mediator mediator = new Mediator.Mediator();
DeviceA deviceA = new DeviceA(mediator);
DeviceB deviceB = new DeviceB(mediator);
mediator.DeviceA = deviceA;
mediator.DeviceB = deviceB;
deviceA.Send("发送消息");
deviceB.Send("发送消息");

Out:

Get message in B: 发送消息
Get message in A: 发送消息
posted @ 2016-07-28 22:58  4Thing  阅读(134)  评论(0编辑  收藏  举报