CS FundamentalsHardCognizant · Capgemini pattern
A notification system has a base Notifier.send(message) and wrappers SMSDecorator, SlackDecorator that each wrap a Notifier and add their own channel's send on top of the wrapped one's send, all implementing the same Notifier interface. How is this different from simply subclassing Notifier into SMSNotifier and SlackNotifier?
- A. There is no real difference; both approaches scale identically as channels are added
- B. Subclassing allows runtime combination but Decorator only works with a single fixed channel
- C. Decorator requires modifying the Notifier interface for every new channel added
- D. Decorator lets behaviours be combined at runtime in any combination (e.g. SMS + Slack together by nesting wrappers) without creating a new subclass for every combination; static subclassing would need a separate class for each combination (SMSAndSlackNotifier, etc.), which grows combinatorially
Correct answer: D. Decorator lets behaviours be combined at runtime in any combination (e.g. SMS + Slack together by nesting wrappers) without creating a new subclass for every combination; static subclassing would need a separate class for each combination (SMSAndSlackNotifier, etc.), which grows combinatorially
Decorator's main benefit over subclassing is avoiding a combinatorial explosion of subclasses: wrapping a base Notifier in several decorators composes behaviours dynamically, while inheritance would need one subclass per combination of channels.
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 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…
- A password field must be 8–20 characters long. Using boundary value analysis on this single range, which set of test lengths is the…
- A use case diagram shows a base use case 'Place Order' with an arrow labelled <<extend>> pointing from 'Apply Discount Code' to 'Place…