Design Mode 22: Mediator Pattern (Mediation Mode)

Source: Internet
Author: User
Document directory
  • 1. Definition of Mediator Pattern
  • Ii. UML class diagram
  • Iii. Mediation mode instance code
  • 4. Another instance code using the mediation Mode

Http://www.dofactory.com/Patterns/PatternMediator.aspx.

1. Definition of Mediator Pattern

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.

Defines how an object encapsulates a series of actions on which objects interact. By avoiding an object pointing to other objects explicitly, the mediator promotes free coupling and allows you to change the interaction between them separately.

Ii. UML class diagram

  • Mediator(Ichatroom)
    • Define an interface for communicating with a colleague-level object
  • Concretemediator(Chatroom)
    • Mutual collaboration through peer-to-peer with colleagues
    • Knows and maintains its own colleague-level objects
  • Colleague classes(Particle ant)
    • Each colleague-level object knows its own regulator object
    • Each colleague-level object must communicate with the regulator when communicating with another colleague-level object.
    Iii. Mediation mode instance code

    Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace mediator_pattern <br/> {<br/> // <summary> </P> <p> // mainapp startup class for structural </P> <p> // mediator design pattern. </P> <p> // </Summary> </P> <p> class mainapp <br/>{</P> <p> // <Summary> </P> <p> // entry point into console application. </P> <p> // </Summary> </P> <p> stati C void main () <br/>{</P> <p> concretemediator M = new concretemediator (); </P> <p> concretecolleague1 C1 = new concretecolleague1 (m); </P> <p> concretecolleague2 C2 = new concretecolleague2 (m ); </P> <p> M. colleague1 = C1; </P> <p> M. colleague2 = c2; </P> <p> c1.send ("how are you? "); </P> <p> c2.send (" Fine, thanks "); </P> <p> // wait for user </P> <p> console. readkey (); </P> <p >}</P> <p> // <summary> </P> <p> // 'mediator' abstract class </P> <p> // </Summary> </P> <p> abstract class mediator <br/>{</P> <p> public abstract void send (string message, </P> <p> colleague ); </P> <p >}</P> <p> // <summary> </P> <p> // The 'concretemediator' class </P> <p> // </Summary> </P> <p> class concretemediator: mediator <br/>{</P> <p> private concretecolleague1 _ colleague1; </P> <p> private concretecolleague2 _ colleague2; </P> <p> Public concretecolleague1 colleague1 <br/>{</P> <p> set {_ colleague1 = value ;} </P> <p >}</P> <p> Public concretecolleague2 colleague2 <br/>{</P> <p> set {_ colleague2 = value ;} </P> <p >}</P> <p> Public override void send (string message, </P> <p> colleague) <br/>{</P> <p> If (colleague ==_ colleague1) <br/>{</P> <p> _ colleague2.notify (Message ); </P> <p >}</P> <p> else <br/> {</P> <p> _ colleague1.y y (Message ); </P> <p >}</P> <p> // <summary> </P> <p> // The 'colleague' abstract class </P> <p> // </Summary> </P> <p> abstract class collegou <br/ >{</P> <p> protected Mediator; </P> <p> // constructor </P> <p> Public colleague (mediator) <br/>{</P> <p> This. mediator = Mediator; </P> <p >}</P> <p> // <summary> </P> <p> // 'concretecolleagu' class </P> <p> // </Summary> </P> <p> class concretecolleague1: colleague <br/>{</P> <p> // constructor </P> <p> Public concretecolleague1 (mediator) </P> <p>: base (mediator) <br/>{</P> <p >}</P> <p> Public void send (string message) <br/>{</P> <p> mediator. send (message, this); </P> <p >}</P> <p> Public void y (string message) <br/>{</P> <p> console. writeline ("colleague1 gets message:" </P> <p> + message ); </P> <p >}</P> <p> // <summary> </P> <p> // 'concretecolleagu' class </P> <p> // </Summary> </P> <p> class concretecolleague2: colleague <br/>{</P> <p> // constructor </P> <p> Public concretecolleague2 (mediator) </P> <p>: base (mediator) <br/>{</P> <p >}</P> <p> Public void send (string message) <br/>{</P> <p> mediator. send (message, this); </P> <p >}</P> <p> Public void y (string message) <br/>{</P> <p> console. writeline ("colleague2 gets message:" </P> <p> + message ); </P> <p >}< br/>

    4. Another instance code using the mediation Mode

    This real-world code demonstrates the Mediator Pattern facilitating loosely coupled communication between different participant ipants registering with a chatroom. 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 wocould be trivial to change to one-to-know.

    Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace mediator_pattern <br/> {<br/> // <summary> </P> <p> // mainapp startup class for real-time- world </P> <p> // mediator design pattern. </P> <p> // </Summary> </P> <p> class mainapp <br/>{</P> <p> // <Summary> </P> <p> // entry point into console application. </P> <p> // </Summary> </P> <p> stati C void main () <br/>{</P> <p> // create chatroom </P> <p> chatroom = new chatroom (); </P> <p> // create maid and register them </P> <p> fig George = new beatle ("George "); </P> <p> maid Paul = new beatle ("PAUL"); </P> <p> maid Ringo = new beatle ("ringo "); </P> <p> maid John = new beatle ("John"); </P> <p> maid Yoko = new nonbeatle ("Yoko "); </P> <p> chatroom. regist Er (George); </P> <p> chatroom. register (Paul); </P> <p> chatroom. register (Ringo); </P> <p> chatroom. register (John); </P> <p> chatroom. register (Yoko); </P> <p> // chatting maid </P> <p> Yoko. send ("John", "Hi John! "); </P> <p> Paul. send ("ringo", "All you need is love"); </P> <p> Ringo. send ("George", "My sweet Lord"); </P> <p> Paul. send ("John", "can't buy me love"); </P> <p> John. send ("Yoko", "My sweet love"); </P> <p> // wait for user </P> <p> console. readkey (); </P> <p >}</P> <p> // <summary> </P> <p> // 'mediator' abstract class </P> <p> // </Summary> </P> <p> abstract class abstractchatroom <br/>{</P> <p> Publ IC abstract void register (maid); </P> <p> public abstract void send (</P> <p> string from, string to, string message ); </P> <p >}</P> <p> // <summary> </P> <p> // The 'concretemediator' class </P> <p> // </Summary> </P> <p> class chatroom: abstractchatroom <br/>{</P> <p> private dictionary <string, particle ant> _ participant ipants = </P> <p> New Dictionary <string, particle ant> (); </P> <p> Public ov Erride void register (maid) <br/>{</P> <p> If (! _ Maid. containsvalue (participant ipant) <br/>{</P> <p> _ participant ipants [participant. name] = maid; </P> <p >}</P> <p> maid. chatroom = This; </P> <p >}</P> <p> Public override void send (</P> <p> string from, string to, string message) <br/>{</P> <p> maid [to]; </P> <p> If (maid! = NULL) <br/>{</P> <p> Participant. receive (from, message ); </P> <p >}</P> <p> // <summary> </P> <p> // The 'abstractcolleagu' class </P> <p> // </Summary> </P> <p> class particle <br/> {</P> <p> private chatroom _ chatroom; </P> <p> private string _ name; </P> <p> // constructor </P> <p> Public particle ant (string name) <br/>{</P> <p> This. _ name = Name; </P> <p >}</P> <p> // gets participant name </P> <p> Public string name <br/>{</P> <p> get {return _ name ;} </P> <p >}</P> <p> // gets chatroom </P> <p> Public chatroom <br/>{</P> <p>> set {_ chatroom = value ;} </P> <p> get {return _ chatroom ;} </P> <p >}</P> <p> // sends message to given participant ipant </P> <p> Public void send (string to, string message) <br/>{</P> <p> _ chatroom. send (_ name, to, message ); </P> <p >}</P> <p> // receives message from given participant ipant </P> <p> Public Virtual void receive (</P> <p>> string from, string message) <br/>{</P> <p> console. writeline ("{0} to {1}: '{2}'", </P> <p> from, name, message ); </P> <p >}</P> <p> // <summary> </P> <p> // 'concretecolleagu' class </P> <p> // </Summary> </P> <p> class Beatle: particle <br/>{</P> <p> // constructor </P> <p> Public beatle (string name) </P> <p>: base (name) <br/>{</P> <p >}</P> <p> Public override void receive (string from, string message) <br/>{</P> <p> console. write ("to a Beatle:"); </P> <p> base. receive (from, message ); </P> <p >}</P> <p> // <summary> </P> <p> // 'concretecolleagu' class </P> <p> // </Summary> </P> <p> class nonbeatle: particle <br/>{</P> <p> // constructor </P> <p> Public nonbeatle (string name) </P> <p>: base (name) <br/>{</P> <p >}</P> <p> Public override void receive (string from, string message) <br/>{</P> <p> console. write ("to a non-Beatle:"); </P> <p> base. receive (from, message); </P> <p >}< br/>}

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.