CS FundamentalsHardInfosys · Product placement
A report class defines generate() as: fetchData(); format(); export(). Subclasses PdfReport and CsvReport override only format() and export(); generate() itself is final. Which pattern is this, and how does it vary behaviour?
- A. Strategy, using composition
- B. Facade, because generate() hides three calls
- C. Factory Method, because subclasses create objects
- D. Template Method, using inheritance: the base class fixes the algorithm's skeleton and subclasses fill in steps
Correct answer: D. Template Method, using inheritance: the base class fixes the algorithm's skeleton and subclasses fill in steps
Template Method fixes the order of steps in a base-class method and lets subclasses override the variable steps (hooks). Strategy would instead pass a formatter object in, varying behaviour through composition at run time.
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
- An app must create a consistent family of UI widgets (Button, Checkbox, Menu) for either a Light or a Dark theme, never mixing a Light…
- A game renders 1,000,000 trees. Each tree object stores mesh and texture data (about 2 MB, the same for every oak) plus its own x, y…
- An editor must save snapshots of a document's internal state so it can be restored later, without exposing the document's private fields…
- In an air-traffic system, 20 aircraft objects would each need references to all the others to coordinate landings. Routing every message…
- How many of these differences between Proxy and Decorator are correct? 1. Both implement the same interface as the object they wrap. 2. A…
- A class Invoice has methods calculateTotal(), saveToDatabase() and renderPdf(). Which principle does it violate, and what is the usual fix?