Usage (Client Code): ● In the Main method, a NewsPublisher is created, along with two subscribers: Alice?
Short answer: And 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… nd Bob. The subscribers…… are subscribed to the publisher. When the publisher sends…
Explain a bit more
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: And Bob. The subscribers are subscribed to the publisher. When the publisher sends out a…
Real-world example (ShopNest)
When order status changes, observers update email, SMS, and analytics without the Order class knowing each one.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png