C # design pattern Series 5-programmer's fate in Mediator intermediary Mode

Source: Internet
Author: User

1. theoretical definition

The intermediary mode defines a one-to-many operation, freeing objects from multiple-to-many reference dependencies, and the communication between all objects is encapsulated in the intermediary.

2. Application Example

Requirement Description: Tanabata is coming. Who said that programmers must be single and hard-pressed? programmers also have their own happiness.

We are also pursuing our own fate. We are all groups with ideals, cultures, and opinions,

Our efforts for the information society can bring us good luck on the Chinese Valentine's Day. Say goodbye to Singles' Day and start action!

1. The three programmers found the fate "YuanFen" and hoped to find their other half,

2. Six beautiful women: Liu Shishi, Jiang Qinqin, Bai fumei, Julie, Shu Qi, and Yu also found the fate of YuanFen,

I hope that the white horse Prince will be able to give himself a clear eye.

3. The fate Guru accepted requests from programmers and beautiful women.

4. Start the fate party!

3. Specific Encoding

1. Girls (Grils)

Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. mediator {public class Girls {// <summary> // beauty Name // </summary> public string Name {get; set ;} /// <summary> /// find an intermediary named "fate" as a matchmaker. // </summary> public YuanFen {get; set ;} /// <summary> /// send a message to your desired Prince Charming /// </summary> /// <param name = "name_message"> name and information </param> public void SendMessage (Dictionary <string, string> name_message) {YuanFen. sendToBoys (name_message);} // <summary> // receives a response from Prince Charming. // </summary> public void RecieveMessage (string msg) {Console. writeLine (msg );}}}

2. Handsome programmers

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. mediator {/// <summary> /// handsome guy 1 /// </summary> public class Boys {/// <summary> // handsome guy name /// </ summary> public string Name {get; set ;}/// <summary> /// an intermediary named "fate" is a matchmaker. // </summary> public YuanFen {get; set ;} /// <summary> /// send a message to your white snow princess. /// </summary> /// <param name = "name_message"> name and information </param> public void SendMessage (Dictionary <string, string> name_message) {YuanFen. sendToGrils (name_message) ;}/// <summary> /// receives a reply from Snow White /// </summary> public void RecieveMessage (string msg) {Console. writeLine (msg );}}}

3. Fate

Using System; using System. collections. generic; using System. linq; using System. text; namespace Com. design. gof. mediator {public class YuanFen {public List <Girls> Girls {get; set;} public List <Boys> Boys {get; set ;} /// <summary> /// send a mail to a girl /// </summary> /// <param name = "name_message"> </param> public void SendToGrils (Dictionary <string, string> name_message) {foreach (var k in name_message) {foreach (var girl in Girls) {if (k. key = girl. name) {girl. recieveMessage (k. value );}}}} /// <summary> // The girl sends a mail to the boy /// </summary> /// <param name = "name_message"> </param> public void SendToBoys (Dictionary <string, string> name_message) {foreach (var k in name_message) {foreach (var boy in Boys) {if (k. key = boy. name) {boy. recieveMessage (k. value );}}}}}}

4. Main Function (party, send a love letter)

Using System; using System. collections. generic; using System. linq; using System. text; using Com. design. gof. mediator; namespace Com. design. gof. test {class Program {static void Main (string [] args) {// fate this matchmaker comes YuanFen yuanFen = new YuanFen {Boys = new List <Boys> (), girls = new List <Girls> ()}; // register with Boys and Girls Boys boy1 = new Boys {Name = "Programmer 1", YuanFen = yuanFen }; boys boy2 = new Boys {Name = "programmer 2", YuanFen = YuanFen}; Boys boy3 = new Boys {Name = "Programmer 3", YuanFen = yuanFen}; // fate accepted yuanFen, the boys' wish. boys. add (boy1); yuanFen. boys. add (boy2); yuanFen. boys. add (boy3); // Girls are also very active Girls girls1 = new Girls {Name = "Liu Shishi", YuanFen = yuanFen }; girls girls2 = new Girls {Name = "Bai fumei", YuanFen = yuanFen}; Girls girls3 = new Girls {Name = "Jiang Qinqin", YuanFen = yuanFen }; girls girls4 = new Girls {Name = "Shu Qi", YuanFen = yua NFen}; Girls girls5 = new Girls {Name = "Julie", YuanFen = yuanFen}; Girls girls6 = new Girls {Name = "", YuanFen = yuanFen }; // fate accepted the love of the beautiful women Cupid yuanFen. girls. add (girls1); yuanFen. girls. add (girls2); yuanFen. girls. add (girls3); yuanFen. girls. add (girls4); yuanFen. girls. add (girls5); yuanFen. girls. add (girls6); // start party // Programmer 1 Dictionary interested in Liu Shishi and Jiang Qinqin <string, string> name_message = new Dictionary <stri Ng, string> (); name_message.Add ("Liu Shishi", boy1.Name + "to Liu Shishi: I love you, like a mouse and rice"); name_message.Add ("Jiang Qinqin ", boy1.Name + "said to Jiang Qinqin: I hope you will be the last person you see before going to bed every day ...... "); boy1.SendMessage (name_message); name_message.Clear (); Console. writeLine (); // programmer 2 interested in Liu Shishi, Julie, and Shu Qi name_message.Add ("", boy2.Name + ": the Earth is still turning, and the world is still changing, and I will always love you "); name_message.Add (" Julie ", boy2.Name +" said to Julie: every day... miss you... "); name_message.Add (" Shu Qi ", Boy2.Name + "said to Shu Qi: you pulled me out of loneliness in the lost heart"); boy2.SendMessage (name_message); name_message.Clear (); Console. writeLine (); // The semantics is of interest to all three programmers name_message.Add ("Programmer 1", girls6.Name + "to Programmer 1: think about the joy of watching sunrise and sunset with me !... Xi "); name_message.Add (" programmer 2 ", girls6.Name +" to programmer 2: My world is only known to you .... "); name_message.Add (" Programmer 3 ", girls6.Name +" said to Programmer 3: because you know you can't live, you will cherish "); girls6.SendMessage (name_message); name_message.Clear (); Console. writeLine (); // Jiang Qinqin is very fond and dedicated. He only sends name_message.Add ("Programmer 1", girls3.Name + "to Programmer 1: Love you forever, your heartbeat is only visible to me "); girls3.SendMessage (name_message); Console. readKey ();}}}

5. Running result

6. Summary

The Chinese Valentine's day is coming.

The attachment contains the program source code.

Download)

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.