Recently I wrote many posts on Data Science and how it’s implemented in .Net.  I thought of writing a blog post on Design Pattern.

So, here it is Template Method Design Pattern. Let’s see what is the template design patterns and when to use it.

The template design pattern belongs to the behavioral group of the pattern from Gang of Four and is applied when a skeleton of an algorithm is defined but some steps are deferred to the subclass.

The structure and flow of the algorithm remain static, but the details of the steps are deferred to subclasses.

The figure below shows the UML representation of the Template Method pattern.

TP6

The AbstractClass defines a skeleton process workflow with abstract steps that ConcreteClassA and ConcreteClassB override and implement. This enables the details of an algorithm to alter depending on the subclasses but allow the structure to remain consistent.

ConcreteClassA and ConcreteClassB inherit from the AbstractClass, implement the abstract methods, and give the details to the algorithm.

To better understand this pattern, let us apply the Template pattern to a system that handles order returns at an e-commerce site. In return process, for each order return, there is a series of processes occur that differ slightly depending on the type of return that is being processed. Here the order returns come in two flavors: no quibbles return and faulty order return.

TP1

Let us create the initial domain model. ReturnAction enumeration enables you to determine which type of return order is being processed. The ReturnOrder entity represents the customer’s order being returned. The Action property determines what type of return order it is, the PaymentTransactionId refers to the original payment used to purchase the order, and the PricePaid and PosatageCost refer to the Order total and shipping costs, respectively. The ProductId holds the unique identifier of the product being returned. Finally, the AmountToRefund is set; this is the amount to be refunded to the customer.

With the domain  model created, we will implement the abstract template method  that will be overridden by the specific faulty and no quibbles subclasses as shown below

TP2

The class and the first two methods are abstract and are required to be implemented by a subclass. The third method simply calls the two abstract methods and passes a ReturnOrder entity as an argument.

TP3

Using Factory method ReturnProcessFactory we will obtain the correct processing class based on the type of order being returned. This class hides the complexity from any client and ensures that logic is contained in one place and is the responsibility of the Factory class.

TP4

Service class coordinates the process of returning an item. The service class has one simple Process method that takes a ReturnOrder as an argument. The service first obtains a ReturnProcessTemplate implementation from the factory, passing in the ReturnOrder entity, and in turn, calls the Process method on the ReturnProcessTemplate. The call returns the item using the sub class’s method and calculates the amount that the customer is entitled to receive.

TP5

The template method pattern is useful when you want to centralize code common to a series of subclasses. To achieve this, you separate the code that varies from that is similar; this enables us to avoid duplication and better maintenance of the code.

Steps for Creating Template Method Pattern

  • Understand the algorithm and decide which steps are standard and which steps are peculiar to each of the current classes.
  • Define a new abstract base class.
  • Define the steps of the algorithm (now called the “template method”)
  • Define a method in the base class for each step that requires many different implementations. This method can have a default implementation – or – it can be defined as abstract.
  • Invoke the method(s) from the template method.
  • Each of the existing classes declares an “is-a” relationship to the new abstract base class.
  • Remove from the existing classes all the implementation details that have been moved to the base class.
  • The only details that will remain in the existing classes will be the implementation details specific to each derived class.

When to use Template Method Pattern?

  • We can use the Template Method pattern in the following cases :
  • When the behavior of an algorithm can change and subclasses implement the behavior through overriding.
  • When we want to avoid code duplication, implementing variations of the algorithm in subclasses.
  • Template Method may not be an obvious choice in the beginning. The general indication that we should use the pattern is when we find that we have two almost identical classes working on some logic. At that stage, we should consider the power of the template method pattern to clean up our code.

 

Please feel free to comment your opinion about this article or whatever you feel like telling me. Also if you like this article, don’t forget to share this article with your friends. Thanks!

Happy Coding !!

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.