• 设计模式(16)中介者模式


    模式介绍

    中介器设计模式定义了一个对象,该对象封装了一组对象之间如何相互作用。

    示例

    我们想象一下电影院的快餐柜,电影院有多个区域都有这样的快餐柜,我们来设计一个它们之间交流的系统。

    中介者接口:

    /// <summary>
    /// The Mediator interface, which defines a send message method which the concrete mediators must implement.
    /// </summary>
    interface Mediator
    {
        void SendMessage(string message, ConcessionStand concessionStand);
    }
    

    抽象的柜台:

    /// <summary>
    /// The Colleague abstract class, representing an entity involved in the conversation which should receive messages.
    /// </summary>
    abstract class ConcessionStand
    {
        protected Mediator mediator;
    
        public ConcessionStand(Mediator mediator)
        {
            this.mediator = mediator;
        }
    }
    

    具体的柜台:

    /// <summary>
    /// A Concrete Colleague class
    /// </summary>
    class NorthConcessionStand : ConcessionStand
    {
        // Constructor
        public NorthConcessionStand(Mediator mediator) : base(mediator)
        {
        }
    
        public void Send(string message)
        {
            Console.WriteLine("North Concession Stand sends message: " + message);
            mediator.SendMessage(message, this);
        }
    
        public void Notify(string message)
        {
            Console.WriteLine("North Concession Stand gets message: "  + message);
        }
    }
    
    /// <summary>
    /// A Concrete Colleague class
    /// </summary>
    class SouthConcessionStand : ConcessionStand
    {
        public SouthConcessionStand(Mediator mediator) : base(mediator)
        {
        }
    
        public void Send(string message)
        {
            Console.WriteLine("South Concession Stand sends message: " + message);
            mediator.SendMessage(message, this);
        }
    
        public void Notify(string message)
        {
            Console.WriteLine("South Concession Stand gets message: " + message);
        }
    }
    

    具体的中介者:

    /// <summary>
    /// The Concrete Mediator class, which implement the send message method and keep track of all participants in the conversation.
    /// </summary>
    class ConcessionsMediator : Mediator
    {
        private NorthConcessionStand _northConcessions;
        private SouthConcessionStand _southConcessions;
    
        public NorthConcessionStand NorthConcessions
        {
            set { _northConcessions = value; }
        }
    
        public SouthConcessionStand SouthConcessions
        {
            set { _southConcessions = value; }
        }
    
        public void SendMessage(string message, ConcessionStand colleague)
        {
            if (colleague == _northConcessions)
            {
                _southConcessions.Notify(message);
            }
            else
            {
                _northConcessions.Notify(message);
            }
        }
    }
    

    客户端调用:

    static void Main(string[] args)
    {
        ConcessionsMediator mediator = new ConcessionsMediator();
    
        NorthConcessionStand leftKitchen = new NorthConcessionStand(mediator);
        SouthConcessionStand rightKitchen = new SouthConcessionStand(mediator);
    
        mediator.NorthConcessions = leftKitchen;
        mediator.SouthConcessions = rightKitchen;
    
        leftKitchen.Send("Can you send some popcorn?");
        rightKitchen.Send("Sure thing, Kenny's on his way.");
    
        rightKitchen.Send("Do you have any extra hot dogs?  We've had a rush on them over here.");
        leftKitchen.Send("Just a couple, we'll send Kenny back with them.");
    
        Console.ReadKey();
    }
    

    总结

    中介者使中介人“站在”通信对象之间,控制他们的通信。常用于聊天系统。

    源代码

    https://github.com/exceptionnotfound/DesignPatterns/tree/master/Mediator

    原文

    https://www.exceptionnotfound.net/mediator-the-daily-design-pattern/

  • 相关阅读:
    iOS加载动态图的两种方法
    python初探
    博客园封笔
    office的分栏技巧
    关于排序...
    LaTex 学习笔记
    vim 学习笔记
    iOS 编程学习笔记之Foundation框架
    数论
    扫描线概览
  • 原文地址:https://www.cnblogs.com/talentzemin/p/9913790.html
Copyright © 2020-2023  润新知