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();
            }
        }
    }
    
  • 相关阅读:
    【objective-c】字典快速转换为Model代码
    【objective-c】类目 延展 协议
    【objective-c】内存管理原则
    UI基础-UI基础控件(一)
    OC面向对象-OC基础早知道
    我对于编程培训班的一些看法
    如何为SQLSERVER查询分析器开启事务
    准备在博客园常驻了
    Spring学习(二)(1.Spring依赖注入的方式 2.依赖注入的类型 3.Bean的作用域 4.自动注入 5在Spring配置文件中引入属性文件6使用注解的方式 )
    Springmvc的常用注解
  • 原文地址:https://www.cnblogs.com/wucg/p/2212541.html
Copyright © 2011-2022 走看看