Usage (Client Code): ● In the Main method, a NewsPublisher is created, along with two subscribers: Alice?
nd Bob. The subscribers are subscribed to the publisher. When the publisher sends
out a notification (Notify()), all subscribers are automatically notified and updated
with the latest news.
class Program
{
static void Main()
{
var publisher = new NewsPublisher();
var subscriber1 = new NewsSubscriber("Alice");
var subscriber2 = new NewsSubscriber("Bob");
publisher.Subscribe(subscriber1);
publisher.Subscribe(subscriber2);
publisher.Notify("Breaking News: Observer Pattern in C#!");
}
}
Output:
lice received news: Breaking News: Observer Pattern in C#!
Bob received news: Breaking News: Observer Pattern in C#!
- How It Works: