CS FundamentalsHardAccenture · Tech Mahindra pattern

An event bus lets any number of modules call bus.subscribe("orderPlaced", handler) and the bus invokes every registered handler when publish("orderPlaced", data) is called, with publishers never knowing which or how many handlers exist. Which pattern is this, and what is its main risk compared to direct method calls?

  1. A. Chain of Responsibility: that pattern passes a request along a chain until one handler accepts it, stopping further propagation
  2. B. Observer (publish-subscribe): it decouples publishers from subscribers, but debugging is harder since the full set of side effects triggered by one publish() call is not visible at the call site
  3. C. Mediator: a mediator would require the bus to know about and explicitly coordinate each subscriber's business logic
  4. D. Command: Command wraps individual requests as objects, it doesn't broadcast to multiple listeners

Correct answer: B. Observer (publish-subscribe): it decouples publishers from subscribers, but debugging is harder since the full set of side effects triggered by one publish() call is not visible at the call site

This is the Observer/pub-sub pattern: a one-to-many broadcast with full decoupling. The classic downside is reduced traceability — since any number of unseen handlers may react, understanding a single publish() call's full effect requires knowing every subscriber.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic