-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (73 loc) · 1.87 KB
/
Program.cs
File metadata and controls
89 lines (73 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
namespace Mediator;
public interface IChatMediator
{
void Register(Participant participant);
void Send(string from, string message);
}
public class ChatRoom : IChatMediator
{
private readonly Dictionary<string, Participant> participants = [];
public void Register(Participant participant)
{
participants[participant.Name] = participant;
participant.Mediator = this;
}
public void Send(string from, string message)
{
foreach (var participant in participants.Values)
{
if (participant.Name == from)
{
continue;
}
participant.Receive(from, message);
}
}
}
public abstract class Participant(string name)
{
public string Name { get; } = name;
internal IChatMediator? Mediator { get; set; }
public void Send(string message)
{
Mediator?.Send(Name, message);
}
public abstract void Receive(string from, string message);
}
public class Developer(string name) : Participant(name)
{
public override void Receive(string from, string message)
{
Console.WriteLine($"[Dev] {from}: {message}");
}
}
public class Tester(string name) : Participant(name)
{
public override void Receive(string from, string message)
{
Console.WriteLine($"[QA] {from}: {message}");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Mediator Pattern Demo ===");
Console.WriteLine();
var chat = new ChatRoom();
var dev = new Developer("Alice");
var tester = new Tester("Bob");
var lead = new Developer("Carol");
chat.Register(dev);
chat.Register(tester);
chat.Register(lead);
dev.Send("Unit tests are red.");
tester.Send("Logging a bug for repro steps.");
lead.Send("Thanks, pushing a fix now.");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}