-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserverPatternExampleWithCategory.cs
More file actions
37 lines (27 loc) · 1.26 KB
/
ObserverPatternExampleWithCategory.cs
File metadata and controls
37 lines (27 loc) · 1.26 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
using Core.Extensions;
using GofConsoleApp.Examples.Behavioral.ObserverPattern.Components;
using GofPatterns.Behavioral.ObserverPattern;
namespace GofConsoleApp.Examples.Behavioral.ObserverPattern;
internal class ObserverPatternExampleWithCategory : BaseExample
{
private readonly IPublisher<string, EnumTopic> newsPublisher = new Publisher<string, EnumTopic>();
protected override bool Execute()
{
var johnDoe = new PersonJohnDoe(Logger);
var andrewSmith = new PersonAndrewSmith(Logger);
newsPublisher.AddSubscriber(johnDoe, EnumTopic.Sports);
newsPublisher.AddSubscriber(andrewSmith, EnumTopic.Sports);
newsPublisher.AddSubscriber(johnDoe, EnumTopic.Politics);
newsPublisher.AddSubscriber(andrewSmith, EnumTopic.Weather);
newsPublisher.AddSubscriber(johnDoe, EnumTopic.Holidays);
do
{
Logger.LogInfoQuit();
var topic = AcceptInputEnum(EnumTopic.Invalid, nameof(EnumTopic), EnumTopic.Invalid);
if (IsInvalidOrQuit(topic, EnumTopic.Invalid, EnumTopic.Quit, out _))
return true;
var newsUpdate = AcceptInputString($"{topic} news update");
newsPublisher.NotifySubscribers(newsUpdate, topic);
} while (true);
}
}