Please enable Javascript to correctly display the contents on Dot Net Tricks!

Template Method Design Pattern - C#

 Print 
  Author : Shailendra Chauhan
Posted On : 24 Feb 2017
Total Views : 4,574   
Updated On : 24 Feb 2017
 

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?

This 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 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 inherits the AbstractClass 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.

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.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS
19 AUG
MEAN Stack 2 (Online)
5:30 Pm - 7:00 PM IST (+5:30 GMT)
19 AUG
ASP.NET MVC with Angular4 (Online)
10:30 Am to 12:30 PM IST (+ 5:30 GMT)
19 AUG
NodeJS with Angular4 (Classroom)
05:30 PM to 07:00 PM IST (+5:30 GMT)
12 AUG
Angular2 and Angular4 (Online)
08:00 AM - 10:00 AM IST(+5.30 GMT)
12 AUG
ASP.NET MVC with Angular4 (Classroom)
09:30 AM - 11:00 AM IST (+5:30GMT)
4 AUG
NodeJS with Angular4 (Online)
08:00 PM - 09:30 PM IST(+5.30 GMT)
26 JUL
ASP.NET MVC with Angular4 (Online)
07:00 AM - 09:00 AM IST(+5.30 GMT)
22 JUL
MEAN Stack 2 (Classroom)
04:00 PM - 05:30 PM IST(+5:30GMT)
20 JUL
Hadoop cum Administration (Online)
07:00 AM - 09:00 AM IST(+5:30 GMT)
15 JUL
ASP.NET MVC with Angular4 (Online)
03:30 PM - 05:30 PM IST(+5.30 GMT)
1 JUL
ASP.NET MVC with Angular4 (Classroom)
08:00 AM - 09:30 AM IST
1 JUL
MEAN Stack 2 (Classroom)
11:00 AM - 12:30 PM IST(+5:30 GMT)
30 JUN
Xamarin Forms : Build Cross-platform Apps (Online)
09:30 PM - 11:00PM IST(+5:30GMT)
6 JUN
ASP.NET MVC with Angular4 (Online)
09:00 PM-11:00 PM IST(+5.30 GMT)
LIKE US ON FACEBOOK
 
+