• 中介者模式 -- 大话设计模式


    在今天,读书有时是件“麻烦”事。它需要你付出时间,付出精力,还要付出一份心境。--仅以《大话设计模式》来祭奠那逝去的……

    中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示的互相引用,从而使其耦合松散,而且可以独立的改变它们之间的交互

    1.美国警告伊拉克

      联合国安全理事会成立,美国伊拉克都加入了理事会。理事会连线美国和伊拉克,在谈判桌上,美国声明:“不准研制核武器,否则要发动战争!”,伊拉克声明:“我们没有核武器,也不怕侵略!”

      定义中介者抽象类--联合国和抽象国家类

        /// <summary>
        /// 联合国(中介者抽象类)
        /// </summary>
        public abstract class Mediator
        {
            /// <summary>
            /// 声明
            /// </summary>
            public abstract void Declare(string message, Country colleague);
        }
    
        /// <summary>
        /// 抽象国家类
        /// </summary>
        public abstract class Country
        {
            protected Mediator mediator;
    
            public Country(Mediator mediator)
            {
                this.mediator = mediator;
            }
        }
    

       定义美国和伊拉克两个国家类,加入联合国

        /// <summary>
        /// 美国
        /// </summary>
        public class USA : Country
        {
            public USA(Mediator mediator) : base(mediator) { }
    
            public void Declare(string message)
            {
                mediator.Declare(message, this);
            }
    
            public void GetMessage(string message)
            {
                Console.WriteLine("美国获得对方消息:" + message);
            }
        }
    
        /// <summary>
        /// 伊拉克
        /// </summary>
        public class Iraq : Country
        {
            public Iraq(Mediator mediator) : base(mediator) { }
    
            public void Declare(string message)
            {
                mediator.Declare(message, this);
            }
    
            public void GetMessage(string message)
            {
                Console.WriteLine("伊拉克获得对方消息:" + message);
            }
        }
    

       定义具体中介者(调停者)--联合国安全理事会

        /// <summary>
        /// 联合国安全理事会
        /// </summary>
        public class ConcreteMediator : Mediator
        {
            public USA USA { get; set; }
    
            public Iraq Iraq { get; set; }
    
            public override void Declare(string message, Country colleague)
            {
                if (colleague == USA)
                {
                    Iraq.GetMessage(message);
                }
                else
                {
                    USA.GetMessage(message);
                }
            }
        }
    

       开启场景模拟

            static void Main(string[] args)
            {
                //联合国安全理事会成立
                ConcreteMediator UNSC = new ConcreteMediator();
    
                //美国加入
                USA c1 = new USA(UNSC);
    
                //伊拉克加入
                Iraq c2 = new Iraq(UNSC);
    
                //理事会连线美国和伊拉克
                UNSC.USA = c1;
                UNSC.Iraq = c2;
    
                //美国声明
                c1.Declare("不准研制核武器,否则要发动战争!");
    
                //伊拉克声明
                c2.Declare("我们没有核武器,也不怕侵略!");
            }
    
  • 相关阅读:
    SQL FORMAT() 函数
    SQL ROUND() 函数
    SQL NOW() 函数
    SQL LEN() 函数
    SQL LCASE() 函数
    SQL MID() 函数
    SQL HAVING 子句
    UCASE() 函数
    SQL GROUP BY 语句
    SQL MAX() 函数
  • 原文地址:https://www.cnblogs.com/amywechat/p/4939491.html
Copyright © 2020-2023  润新知