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();
        }
    }
}


from:http://www.uml.org.cn/sjms/200906113.asp


阅读更多
文章标签: string class null
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭