Design Pattern - Mediator(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
Definition
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
Participants
The classes and/or objects participating in this pattern are:
- Mediator (IChatroom)
- Defines an interface for communicating with Colleague objects
- ConcreteMediator (Chatroom)
- Implements cooperative behavior by coordinating Colleague objects
- Knows and maintains its colleagues
- Colleague classes (Participant)
- Each Colleague class knows its Mediator object
- Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague
Sample Code in C#
This structural code demonstrates the Mediator pattern facilitating loosely coupled communication between different objects and object types. The mediator is a central hub through which all interaction must take place.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Mediator Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; /// <summary> /// Startup class for Structural Mediator Design Pattern. /// </summary> internal static class Program { #region Methods /// <summary> /// Entry point into console application. /// </summary> private static void Main() { var m = new ConcreteMediator(); var c1 = new ConcreteColleague1(m); var c2 = new ConcreteColleague2(m); m.Colleague1 = c1; m.Colleague2 = c2; c1.Send("How are you?"); c2.Send("Fine, thanks"); } #endregion } /// <summary> /// The 'Mediator' abstract class /// </summary> internal abstract class Mediator { #region Public Methods and Operators /// <summary> /// The send. /// </summary> /// <param name="message"> /// The message. /// </param> /// <param name="colleague"> /// The colleague. /// </param> public abstract void Send(string message, Colleague colleague); #endregion } /// <summary> /// The 'ConcreteMediator' class /// </summary> internal class ConcreteMediator : Mediator { #region Fields /// <summary> /// The colleague 1. /// </summary> private ConcreteColleague1 colleague1; /// <summary> /// The colleague 2. /// </summary> private ConcreteColleague2 colleague2; #endregion #region Public Properties /// <summary> /// Sets the colleague 1. /// </summary> public ConcreteColleague1 Colleague1 { set { this.colleague1 = value; } } /// <summary> /// Sets the colleague 2. /// </summary> public ConcreteColleague2 Colleague2 { set { this.colleague2 = value; } } #endregion #region Public Methods and Operators /// <summary> /// The send. /// </summary> /// <param name="message"> /// The message. /// </param> /// <param name="colleague"> /// The colleague. /// </param> public override void Send(string message, Colleague colleague) { if (colleague == this.colleague1) { this.colleague2.Notify(message); } else { this.colleague1.Notify(message); } } #endregion } /// <summary> /// The 'Colleague' abstract class /// </summary> internal abstract class Colleague { #region Fields /// <summary> /// The mediator. /// </summary> protected readonly Mediator Mediator; #endregion // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Colleague"/> class. /// </summary> /// <param name="mediator"> /// The mediator. /// </param> protected Colleague(Mediator mediator) { this.Mediator = mediator; } #endregion } /// <summary> /// A 'ConcreteColleague' class /// </summary> internal class ConcreteColleague1 : Colleague { // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="ConcreteColleague1"/> class. /// </summary> /// <param name="mediator"> /// The mediator. /// </param> public ConcreteColleague1(Mediator mediator) : base(mediator) { } #endregion #region Public Methods and Operators /// <summary> /// The notify. /// </summary> /// <param name="message"> /// The message. /// </param> public void Notify(string message) { Console.WriteLine("Colleague1 gets message: " + message); } /// <summary> /// The send. /// </summary> /// <param name="message"> /// The message. /// </param> public void Send(string message) { this.Mediator.Send(message, this); } #endregion } /// <summary> /// A 'ConcreteColleague' class /// </summary> internal class ConcreteColleague2 : Colleague { // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="ConcreteColleague2"/> class. /// </summary> /// <param name="mediator"> /// The mediator. /// </param> public ConcreteColleague2(Mediator mediator) : base(mediator) { } #endregion #region Public Methods and Operators /// <summary> /// The notify. /// </summary> /// <param name="message"> /// The message. /// </param> public void Notify(string message) { Console.WriteLine("Colleague2 gets message: " + message); } /// <summary> /// The send. /// </summary> /// <param name="message"> /// The message. /// </param> public void Send(string message) { this.Mediator.Send(message, this); } #endregion }}// Output:/*Colleague2 gets message: How are you?Colleague1 gets message: Fine, thanks*/
This real-world code demonstrates the Mediator pattern facilitating loosely coupled communication between different Participants registering with a Chat room. The Chatroom is the central hub through which all communication takes place. At this point only one-to-one communication is implemented in the Chatroom, but would be trivial to change to one-to-many.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Mediator Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; using System.Collections.Generic; /// <summary> /// Startup class for Real-World Mediator Design Pattern. /// </summary> internal static class Program { #region Methods /// <summary> /// Entry point into console application. /// </summary> private static void Main() { // Create chat room var 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"); } #endregion } /// <summary> /// The 'Mediator' abstract class /// </summary> internal abstract class AbstractChatroom { #region Public Methods and Operators /// <summary> /// The register. /// </summary> /// <param name="participant"> /// The participant. /// </param> public abstract void Register(Participant participant); /// <summary> /// The send. /// </summary> /// <param name="from"> /// The from. /// </param> /// <param name="to"> /// The to. /// </param> /// <param name="message"> /// The message. /// </param> public abstract void Send(string from, string to, string message); #endregion } /// <summary> /// The 'ConcreteMediator' class /// </summary> internal class Chatroom : AbstractChatroom { #region Fields /// <summary> /// The participants. /// </summary> private Dictionary<string, Participant> participants = new Dictionary<string, Participant>(); #endregion #region Public Methods and Operators /// <summary> /// The register. /// </summary> /// <param name="participant"> /// The participant. /// </param> public override void Register(Participant participant) { if (!this.participants.ContainsValue(participant)) { this.participants[participant.Name] = participant; } participant.Chatroom = this; } /// <summary> /// The send. /// </summary> /// <param name="from"> /// The from. /// </param> /// <param name="to"> /// The to. /// </param> /// <param name="message"> /// The message. /// </param> public override void Send(string from, string to, string message) { Participant participant = this.participants[to]; if (participant != null) { participant.Receive(from, message); } } #endregion } /// <summary> /// The 'AbstractColleague' class /// </summary> internal class Participant { #region Fields /// <summary> /// The chatroom. /// </summary> private Chatroom chatroom; #endregion // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Participant"/> class. /// </summary> /// <param name="name"> /// The name. /// </param> public Participant(string name) { this.Name = name; } #endregion // Gets chatroom #region Public Properties /// <summary> /// Gets or sets the chatroom. /// </summary> public Chatroom Chatroom { get { return this.chatroom; } set { this.chatroom = value; } } /// <summary> /// Gets the name. /// </summary> public string Name { get; private set; } #endregion #region Public Methods and Operators /// <summary> /// Receives message from given participant. /// </summary> /// <param name="from"> /// The from. /// </param> /// <param name="message"> /// The message. /// </param> public virtual void Receive(string from, string message) { Console.WriteLine("{0} to {1}: '{2}'", from, this.Name, message); } /// <summary> /// Sends message to given participant. /// </summary> /// <param name="to"> /// The to. /// </param> /// <param name="message"> /// The message. /// </param> public void Send(string to, string message) { this.chatroom.Send(this.Name, to, message); } #endregion } /// <summary> /// A 'ConcreteColleague' class /// </summary> internal class Beatle : Participant { // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Beatle"/> class. /// </summary> /// <param name="name"> /// The name. /// </param> public Beatle(string name) : base(name) { } #endregion #region Public Methods and Operators /// <summary> /// The receive. /// </summary> /// <param name="from"> /// The from. /// </param> /// <param name="message"> /// The message. /// </param> public override void Receive(string from, string message) { Console.Write("To a Beatle: "); base.Receive(from, message); } #endregion } /// <summary> /// A 'ConcreteColleague' class /// </summary> internal class NonBeatle : Participant { // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="NonBeatle"/> class. /// </summary> /// <param name="name"> /// The name. /// </param> public NonBeatle(string name) : base(name) { } #endregion #region Public Methods and Operators /// <summary> /// The receive. /// </summary> /// <param name="from">
相關推薦
Design Pattern - Mediator(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Observer(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Memento(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Iterator(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Interpreter(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Command (C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Flyweight(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Proxy(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Facade(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Decorator(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Composite(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Bridge(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Adapter(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Builder(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Prototype(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Singleton(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Strategy(C )
Definition
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from cli
Design Pattern - Chain of Responsibility(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Factory Method(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Abstract Factory(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!