Design Patterns Explained
  • Getting Started
  • SOLID Principles
    • Single Responsibility Principle
    • Open / Closed Principle
    • Liskov Substitution Principle
    • Interface Segregation Principle
    • Dependency Inversion Principle
  • Design Patterns
    • Creational Patterns
      • Abstract Factory Pattern
      • Builder Pattern
      • Factory Pattern
      • Prototype Pattern
      • Singleton Pattern
    • Behavioral Patterns
      • Chain Of Responsibility Pattern
      • Command Pattern
      • Interpreter Pattern
      • Iterator Pattern
      • Mediator Pattern
        • Example 1
      • Memento Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
        • Example 1
      • Template Method Pattern
      • Visitor Pattern
    • Structural Patterns
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Decorator Pattern
      • Facade Pattern
      • Flyweight Pattern
      • Proxy Pattern
  • Roadmap
Powered by GitBook
On this page
  • When to use
  • Structue
  • Note
  • Examples

Was this helpful?

  1. Design Patterns
  2. Behavioral Patterns

Template Method Pattern

PreviousExample 1NextVisitor Pattern

Last updated 4 years ago

Was this helpful?

Define the skeleton of an algorithm, but deferring some steps to subclasses without changing the algorithm's structure.

When to use

  1. Used to implement the invariant parts of an algorithm once, and leave it upto subclasses to implement what varies.

  2. When a common behavior among subclasses should be factored in a common clase to avoid duplication (DRY Principle).

  3. When you want to let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure.

  4. When you need to let the client be able to extend the logic exactly at certain points and no more (using hooks methods).

Structue

  • AbstractClass: defines the primitive operations that concrete subclasses define to implement steps of an algorithm.

  • ConcreteClass: implements the primitive operations to carry out subclass-specific steps of the algorithm.

Note

Usually AbstractClass will have abstract steps methods that should be implemented by the ConcreteClass, but there may also be Hooks Methods that are optional points of extension of the algorithm.

You may close the overriding of some functions if this is available on the language of implementation.

Template Method is a very good example for Open / Closed Principle.

Examples

Source Code

UML

// TODO

// TODO

You can find the tests .

here
Example 1
Example 2