-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathProgram.cs
More file actions
25 lines (19 loc) · 943 Bytes
/
Program.cs
File metadata and controls
25 lines (19 loc) · 943 Bytes
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
/* In this example, MessagePublisher acts as the subject that notifies its
* subscribers (UserSubscriber) whenever a new message is posted.
* Subscribers implement the IObserver interface, allowing them to be notified.
* This pattern decouples the publisher from its subscribers;
* new subscribers can be added or removed at any time without changing the publisher's code,
* adhering to the open/closed principle. */
MessagePublisher publisher = new MessagePublisher();
// Create subscribers
UserSubscriber alice = new UserSubscriber("Alice");
UserSubscriber bob = new UserSubscriber("Bob");
// Subscribe users to receive updates
publisher.Subscribe(alice);
publisher.Subscribe(bob);
// Post a message - all subscribers get notified
publisher.PostMessage("Hello, World!");
// Unsubscribe a user
publisher.Unsubscribe(bob);
// Post another message - only subscribed users get notified
publisher.PostMessage("Second message");