Skip to content

Instantly share code, notes, and snippets.

@miteshsureja
Created March 30, 2018 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miteshsureja/e6290b3001cf5b4ff8a7be0a91f8b113 to your computer and use it in GitHub Desktop.
Save miteshsureja/e6290b3001cf5b4ff8a7be0a91f8b113 to your computer and use it in GitHub Desktop.
Mediator Design Pattern Example
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.");
}
}
}
}
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);
}
}
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();
}
}
}
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();
}
}
}
@miteshsureja
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment