zoukankan      html  css  js  c++  java
  • 转:Mediator模式

    using System;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.Mediator
    {
        //MediatorR
        abstract class AbstractChatroom
        {
            public abstract void Register(Participant participant);
            public abstract void Send(string from, string to, string message);
        }
    
        //ConcreteMediator
        class Chatroom : AbstractChatroom
        {
            private System.Collections.Hashtable participants = new System.Collections.Hashtable();
            public override void Register(Participant participant)
            {
                if (participants[participant.Name] == null)
                {
                    participants[participant.Name] = participant;
                }
                participant.Chatroom = this;
            }
            public override void Send(string from, string to, string message)
            {
                Participant pto = (Participant)participants[to];
                if (pto != null)
                {
                    pto.Receive(from, message);
                }
            }
        }
        /**********************************************************************************/
        //AbstractColleague
        class Participant
        {
            private Chatroom chatroom;
            private string name;
    
            //Constructor
            public Participant(string name)
            {
                this.name = name;
            }
            //Properties
            public string Name
            {
                get { return name; }
            }
            public Chatroom Chatroom
            {
                set { chatroom = value; }
                get { return chatroom; }
    
            }
            public void Send(string to, string message)
            {
                chatroom.Send(name, to, message);
            }
            public virtual void Receive(string from, string message)
            {
                Console.WriteLine("{0} to {1}:'{2}'", from, name, message);
            }
        }
    
    
        //ConcreteColleaguel
        class Beatle : Participant
        {
            //Constructor
            public Beatle(string name)
                : base(name)
            { }
            public override void Receive(string from, string message)
            {
                Console.Write("To a Beatle: ");
                base.Receive(from, message);
            }
        }
    
        //ConcreteColleague2
        class NonBeatle : Participant
        {
            //Constructor
            public NonBeatle(string name)
                : base(name)
            { }
            public override void Receive(string from, string message)
            {
                Console.Write("To a non-Beatle:");
                base.Receive(from, message);
            }
        }
    
        class Program
        {
    
            //客户端调用如下:
            static void Main(string[] args)
            {
                //create chatroom
                Chatroom chatroom = new Chatroom();
                //Create participants and register them
                Participant George = new Beatle("George");
                Participant Paul = new Beatle("Paul");
                Participant Ringo = new Beatle("Ringo");
                Participant John = new NonBeatle("John");
                Participant Yoko = new NonBeatle("Yoko");
                chatroom.Register(George);
                chatroom.Register(Paul);
                chatroom.Register(Ringo);
                chatroom.Register(John);
                chatroom.Register(Yoko);
    
                //chatting participants
                George.Send("Yoko", "你说错话了");
                Yoko.Send("George", "哪里说错了?");
                //Yoko.Send("John", "Hi John");
                //Paul.Send("Ringo", "All you need is love");
                //Ringo.Send("George", "My sweet Lord");
                //Paul.Send("John", "Can't buy me love");
                //John.Send("Yoko", "My sweet love");
                Console.ReadKey();
            }
        }
    }
    
  • 相关阅读:
    odoo12安装Wkhtmltopdf打印出pdf已损坏
    odoo10实现单点登陆绕过登陆集成页面
    odoo添加顶部按钮实现自定义方法
    odoo t标签用法
    C#编写dll进行sql server数据库扩展储存过程
    小程序生成二维码(使用binarywang封装的微信工具包)
    -bash: ./start.sh: /bin/sh^M: bad interpreter: No such file or directory 错误解决方案
    过滤器跟拦截器的区别
    mybatis-DATE_FORMAT() 时间格式化,所对应的时间格式
    解决 MyBatis-Plus 更新对象无法设空值
  • 原文地址:https://www.cnblogs.com/wucg/p/2212541.html
Copyright © 2011-2022 走看看