Which pattern fits 'notify email, analytics and inventory services whenever an order is placed'?
Options
- Observer: subscribers are notified of each new order (Correct option)
- Singleton: one shared service handles every order
- Builder: each order is assembled step by step
- Prototype: each order is cloned from a template
Answer
Observer (publish/subscribe)
Why
Observer: an object (the subject) keeps a list of subscribers and notifies all of them when something happens.
The order code just announces 'order placed'; it does not know who listens. You can add a new listener (e.g. loyalty points) without touching the order code.
In bigger systems the same idea becomes an event bus or message queue.
Remember
Observer = 'tell everyone who subscribed'.