Navratri Sale: Get Upto 30% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Behavioural Design Pattern: Template Method Design Pattern

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:01:00

Define Template Method Design Pattern

This pattern is used to describe an algorithm's core steps and allows for changes to the implementation of the individual steps. This pattern is similar to the strategy design pattern, except it modifies certain elements of an algorithm rather than replacing the entire algorithm.

Template pattern with UML diagram

The classes, interfaces, & objects in the above UML class diagram are defined as follows.

  • AbstractClass: An abstract class with template methods and abstract procedures for each stage that subclasses can implement.
  • ConcreteClassA/B: These subclasses inherit the AbstractClass and override its operations.

When Should You Use the Template Design Pattern?

The template design pattern is beneficial in the following situations:

  • An abstract view of an algorithm is necessary, but implementation differs depending on the subclass.
  • Common subclass behaviours can be utilised to create a common class.

Advantages of the Template Method Design Pattern

  • Code Reusability: Encourages code reuse by allowing common behaviours to be described in a single location (the abstract class).
  • Flexibility and Control: Allows subclasses to redefine certain phases of an algorithm without affecting the structure.
  • Inversion of Control: The parent class controls the algorithm's higher-level phases, while subclasses handle the details.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this