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?

  1. A. Strategy, using composition
  2. B. Facade, because generate() hides three calls
  3. C. Factory Method, because subclasses create objects
  4. 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.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic