Created
March 30, 2018 14:07
-
-
Save miteshsureja/e6290b3001cf5b4ff8a7be0a91f8b113 to your computer and use it in GitHub Desktop.
Mediator Design Pattern Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MediatorPattern | |
{ | |
//Concrete Mediator | |
public class ChatRoom : IChatRoom | |
{ | |
private Dictionary<string, Participant> participants = | |
new Dictionary<string, Participant>(); | |
public void Register(Participant participant) | |
{ | |
if (!participants.ContainsValue(participant)) | |
participants.Add(participant.Name, participant); | |
participant.ChatRoom = this; | |
} | |
public void SendMessage(string from, string to, string message) | |
{ | |
if (participants.Keys.Contains(to)) | |
{ | |
Participant participant = participants[to]; | |
participant.Receive(from, message); | |
} | |
else | |
{ | |
Console.WriteLine("Participant not registered."); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MediatorPattern | |
{ | |
//Mediator | |
public interface IChatRoom | |
{ | |
void Register(Participant participant); | |
void SendMessage(string from, string to, string message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MediatorPattern | |
{ | |
//Participant class (Colleague class) | |
public class Participant | |
{ | |
public Participant(string name) | |
{ | |
Name = name; | |
} | |
public ChatRoom ChatRoom { get; set; } | |
public string Name { get; set; } | |
public void Send(string to, string message) | |
{ | |
ChatRoom.SendMessage(Name, to, message); | |
} | |
public void Receive(string from, string message) | |
{ | |
Console.WriteLine("{0} to {1} - '{2}'", | |
from, Name, message); | |
} | |
} | |
//Client class (entry point) | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ChatRoom chatroom = new ChatRoom(); | |
List<string> people = new List<string>() | |
{ "Mitesh", "Sandeep", "Vikram", "Amit" }; | |
List<Participant> participants = new List<Participant>(); | |
foreach (string s in people) | |
participants.Add(new Participant(s)); | |
foreach (Participant p in participants) | |
chatroom.Register(p); | |
participants.Find(x => x.Name.Equals("Mitesh")). | |
Send("Sandeep", "Hey Sandeep, How are you?"); | |
participants.Find(x => x.Name.Equals("Vikram")). | |
Send("Mitesh", "Where are you?"); | |
participants.Find(x => x.Name.Equals("Sandeep")). | |
Send("Mitesh", "Hi Mitesh, i am good. Thanks"); | |
participants.Find(x => x.Name.Equals("Mitesh")). | |
Send("Vikram", "I am at home"); | |
participants.Find(x => x.Name.Equals("Amit")). | |
Send("Vikram", "Hi"); | |
Console.Read(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MediatorPattern | |
{ | |
//Client class (entry point) | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ChatRoom chatroom = new ChatRoom(); | |
List<string> people = new List<string>() | |
{ "Mitesh", "Sandeep", "Vikram", "Amit" }; | |
List<Participant> participants = new List<Participant>(); | |
foreach (string s in people) | |
participants.Add(new Participant(s)); | |
foreach (Participant p in participants) | |
chatroom.Register(p); | |
participants.Find(x => x.Name.Equals("Mitesh")). | |
Send("Sandeep", "Hey Sandeep, How are you?"); | |
participants.Find(x => x.Name.Equals("Vikram")). | |
Send("Mitesh", "Where are you?"); | |
participants.Find(x => x.Name.Equals("Sandeep")). | |
Send("Mitesh", "Hi Mitesh, i am good. Thanks"); | |
participants.Find(x => x.Name.Equals("Mitesh")). | |
Send("Vikram", "I am at home"); | |
participants.Find(x => x.Name.Equals("Amit")). | |
Send("Vikram", "Hi"); | |
Console.Read(); | |
} | |
} | |
} |
Author
miteshsureja
commented
Mar 30, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment