Switch: mediator Mode

Source: Internet
Author: User
Using system; using system. LINQ; using system. text; namespace designpatterns. mediator {// mediatorr abstract class abstractchatroom {public abstract void register (FIG); public abstract void send (string from, string to, string message);} // concretemediator class chatroom: abstractchatroom {private system. collections. hashtable participant ipants = new system. collections. hashtable (); Pub LIC override void register (maid) {If (maid [particle ant. name] = NULL) {maid [participant. name] = maid;} maid. chatroom = This;} public override void send (string from, string to, string message) {particle ant PTO = (particle ant) maid [to]; If (PTO! = NULL) {PTO. receive (from, message );}}} /*************************************** **************************************** * ** // abstractcollegou class participant ipant {private chatroom; private string name; // constructor Public Participant ipant (string name) {This. name = Name;} // properties public string name {get {return name;} public 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 ipant {// constructor public beatle (string name): Base (name) {} public override void receive (string from, string message) {consol E. write ("to a Beatle:"); base. receive (from, message) ;}/// concretecolleague2 class nonbeatle: FIG {// 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 {// The client calls the following: static void main (string [] ARGs) {// create chatroom = New chatroom (); // create participant ants and register them participant ipant George = new beatle ("George"); participant ipant Paul = new beatle ("PAUL "); particle ant Ringo = new beatle ("ringo"); particle ant John = new nonbeatle ("John"); particle ant Yoko = new nonbeatle ("Yoko"); chatroom. register (George); chatroom. register (Paul); chatroom. register (Ringo); chatroom. register (John); chatroom. register (Yoko); // chattin G participant ipants George. Send ("Yoko", "You are wrong"); Yoko. Send ("George", "what is wrong? "); // 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 ();}}}

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.