⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediatorrealworld.cs

📁 使用C#程序将23个常用设计模式进行列表显示
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Collections;


//中介者模式(Mediator)
//意图
//    用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
//适用性
//    1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
//    2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
//    3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。

namespace DesignPattern.MediatorRealWorld
{
    class MediatorRealWorld : AbstractPattern
    {
        public static void Run(TextBox tbInfo)
        {
            s_tbInfo = tbInfo;
            s_tbInfo.Text = "";

            // 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 Beatle("John") ; 
            Participant Yoko = new NonBeatle("Yoko"); 

            chatroom.Register(George); 
            chatroom.Register(Paul); 
            chatroom.Register(Ringo); 
            chatroom.Register(John); 
            chatroom.Register(Yoko); 
            
            // Chatting participants 
            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") ; 
            
            // Wait for user 
            //Console.Read();
        }
    }
    
    // "Mediator" 
    abstract class AbstractChatroom 
    {
        public abstract void Register(Participant participant); 
        public abstract void Send( string from, string to, string message); 
    }
    
    // "ConcreteMediator" 
    class Chatroom : AbstractChatroom 
    {
        private Hashtable participants = new 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) 
        {
            DesignPattern.FormMain.OutputInfo("{0} to {1}: '{2}'", from, Name, message); 
        }
    }
    
    //" ConcreteColleague1" 
    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); 
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -