Template method Pattern in C#

Template method pattern comes under behavioral design pattern category of Gang of four (GoF) design patterns. This article explains what is template method pattern and how to implement template method pattern in C#.

Template pattern is useful when you want to implement an algorithm but some steps of the algorithm can vary upon the requirement.

Template Method Pattern

Template pattern provides the skeleton to implement an algorithm and the steps to implement the algorithm can be overridden by implementing class i.e. sub-classes can redefine the steps without altering the original structure of the algorithm.

Template pattern contains two main parts: Abstract class and Concrete class. Abstract class provides skeleton to implement an algorithm and also provides steps that may be overridden by concrete class.  The steps to implement the algorithm can be customized by redefining in steps in concrete class.

Gang of Four(GoF) Definition:
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

Template Design Pattern – UML Diagram

template-method-pattern-in-c-uml-diagram

 

Participants in above UML diagram are:

  • AbstractClass: This class defines PrimitiveOperation and TemplateMethod PrimitiveOperations are abstract methods that is implemented by ConcreteClass to redefine the steps of the algorithm. TemplateMethod defines skeleton of the algorithm and this method calls primitive operation methods.
  • ConcreteClass: This class implements AbstractClass and implements primitive operations that defines the steps of algorithm.

Template Method Pattern in C# – Example

Let’s implement a simple example to understand template method pattern in C#.

Problem Statement

Consider a video player which plays different format of videos like mp4, mkv and avi format. To support different formats of video respective video decoder is required.

  • To play a video first video file will be loaded. This step will be common for all video formats, so it will be default state.
  • Once the file will be loaded then it will be processed with suitable video decoder. This step will vary for different video formats.
  • Once the video will be decoded then our media player will starts playing the video. This state is also common for all video formats.

Class Diagram

template-method-design-pattern-in-c-class-diagram

Participants in above class diagram are:

  • VideoPlayer: Abstract class
  • MkvVideo: Concrete class
  • MP4Video: Concrete Class

Template Method Pattern in C# – Implementation Code

Result

template-method-pattern-in-c-output

Hope you get the idea about how to implement template method pattern in C#. Please post your comments for queries and feedback. Thanks for reading this article.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

Your email address will not be published. Required fields are marked *