Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Template Method Design Pattern

Template Method Design Pattern

23 Aug 2022
Intermediate
35.6K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Template Method Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc. In this article, I would like to share what is template method pattern and how is it work?

What is Template Method Design pattern?

Template method pattern is used to define the basic steps of an algorithm and allow the implementation of the individual steps to be changed. This pattern looks similar to the strategy design pattern. The main difference is the ability to change the parts of an algorithm rather than replacing an entire algorithm.

In the Template pattern, an abstract class defined the template method and this method includes the steps which are implemented by the subclasses.

Template Method Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Template Method Design Pattern is given below:

Template Method Design Pattern C#

The classes, interfaces, and objects in the above UML class diagram are as follows:

  1. AbstractClass

    This is an abstract class that contains template method and abstract operations for each of the steps that may be implemented by subclasses.

  2. ConcreteClassA/B

    These are subclasses which inherit the abstract class and override the abstract class operations.

C# - Implementation Code

public abstract class AbstractClass
{
 public void TemplateMethod()
 {
 Step1();
 Step2();
 Step3();
 }
 public abstract void Step1();

 public abstract void Step2();

 public abstract void Step3();
}
public class ConcreteClassA : AbstractClass
{
 public override void Step1()
 {
 Console.WriteLine("Concrete Class A, Step 1");
 }

 public override void Step2()
 {
 Console.WriteLine("Concrete Class A, Step 2");
 }

 public override void Step3()
 {
 Console.WriteLine("Concrete Class A, Step 3");
 }
}
public class ConcreteClassB : AbstractClass
{
 public override void Step1()
 {
 Console.WriteLine("Concrete Class B, Step 1");
 }

 public override void Step2()
 {
 Console.WriteLine("Concrete Class B, Step 2");
 }

 public override void Step3()
 {
 Console.WriteLine("Concrete Class B, Step 3");
 }
}


Real Life Example:

Real Life Example of Template Method Design Pattern C#

When to use it?

  1. An abstract view of an algorithm is required, but implementation varies according to the subclasses.

  2. Common behaviors of subclasses can be used to make to a common class.

Read More Articles Related to Design patterns
What do you think?

I hope you will enjoy the Template Method Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this