Editor

Template Method Pattern

Advanced · Write the code · from Python Design Patterns: Factory, Strategy, Observer, and More

From the lesson: Python Design Patterns: Factory, Strategy, Observer, and More

Design patterns are proven recipes for solving common software problems. Think of them like architectural blueprints -- you wouldn't redesign plumbing from scratch for every house. Patterns give your code a shared vocabulary so other developers instantly understand your intent.

Read the lesson

Create a Report base class using the Template Method pattern:

  • generate() method that calls self.header(), self.body(), self.footer() in order
  • header() prints === Report ===
  • footer() prints === End ===
  • body() should raise NotImplementedError
  • Then create SalesReport(Report) that overrides body() to print Sales: $1000.

    Create a SalesReport instance and call generate().

    # Create Report base class and SalesReport subclass

    More Design Patterns exercises

    All Python exercises · 50 practice problems · Advanced Python quiz