-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
48 lines (37 loc) · 1.11 KB
/
Program.cs
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
IChat chat = new AnonymousChat();
IMessenger bobMessenger = new BobMessenger();
IMessenger aliceMessenger = new AliceMessenger();
chat.AddMessenger(bobMessenger);
chat.AddMessenger(aliceMessenger);
chat.SendMessage(aliceMessenger, "Hello");
chat.SendMessage(bobMessenger, "How are you, Alice");
interface IMessenger
{
void Receiver(string message);
}
interface IChat
{
void SendMessage(IMessenger receiver, string message);
void AddMessenger(IMessenger messenger);
}
class AliceMessenger : IMessenger
{
public void Receiver(string message) => Console.WriteLine($"received the message: {message}");
}
class BobMessenger : IMessenger
{
public void Receiver(string message) => Console.WriteLine($"received the message: {message}");
}
class AnonymousChat : IChat
{
private readonly List<IMessenger> _messengers = new();
public void SendMessage(IMessenger receiver, string message)
{
if (!string.IsNullOrWhiteSpace(message))
{
return;
}
receiver.Receiver(message);
}
public void AddMessenger(IMessenger messenger) => _messengers.Add(messenger);
}