-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
54 lines (43 loc) · 1.31 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
49
50
51
52
53
54
namespace DesignPatterns.Observer;
public enum ChangeType
{
Add, Update, Delete
}
public record Subscriber(string Name)
{
public void Update(Subscriber subscriber, Page page, ChangeType changeType) =>
Console.WriteLine($"Subscriber Name:{{{subscriber.Name}}} Change Type:{{{changeType}}} Page Name:{{{page.PageName}}}");
}
public class Page
{
private string _name;
readonly List<Subscriber> _subscribers = new();
public string PageName => _name;
public void UpdateToPage(string pageName)
{
_name = pageName;
Notify(ChangeType.Update);
}
public void AddToPage(string pageName)
{
_name = pageName;
Notify(ChangeType.Add);
}
public void DeleteToPage() => Notify(ChangeType.Delete);
public void Subscribe(Subscriber subscriber) => _subscribers.Add(subscriber);
private void Notify(ChangeType changeType) =>
_subscribers.ForEach(subscriber=> subscriber.Update(subscriber, this, changeType));
}
class App
{
public static void Main()
{
var page = new Page();
page.Subscribe(new Subscriber("Ibrahim"));
page.Subscribe(new Subscriber("Tom"));
page.Subscribe(new Subscriber("Carton"));
page.AddToPage("test1");
page.UpdateToPage("test2");
page.DeleteToPage();
}
}