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?
- A. Chain of Responsibility: that pattern passes a request along a chain until one handler accepts it, stopping further propagation
- 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
- C. Mediator: a mediator would require the bus to know about and explicitly coordinate each subscriber's business logic
- 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.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- A parser builds an abstract syntax tree where each node type (NumberNode, AddNode, MulNode) implements its own evaluate() method directly…
- A notification system has a base Notifier.send(message) and wrappers SMSDecorator, SlackDecorator that each wrap a Notifier and add their…
- A spreadsheet's Cell objects can contain other Cells (a range), and client code calls cell.getValue() uniformly whether cell is a single…
- A rendering engine has a Shape hierarchy (Circle, Square) and a separate Renderer hierarchy (VectorRenderer, RasterRenderer), and any…
- A text editor needs to support thousands of on-screen Character objects efficiently. Most of a Character's data (font, size, color) is…
- A rule says: a loan is approved only if (credit score >= 700) AND (income >= 50000) AND (no existing default). Using a decision table that…