Python Design Patterns — Template method pattern

Introduction

Template method pattern is one of the behavioral design patterns. I think that you all must have already used this pattern many times.

In this pattern, Abstract class holds the primitive operations which needs to be executed and all those methods might be abstract if they are customizable and it also holds one method which executes all the other methods in pattern which you have defined. This method is also called template method. This abstract class must be overridden by sub classes to write custom primitive operations according to your requirements.

We can understand this pattern by following code.

In this sample code, You can see AbstractClass holds two operation method which are abstract methods and one template_method which executes both the operations. Now if you see RealClass1 overrides two operation methods according to requirements. Further, if you create object of RealClass1 and call template_method method which will execute both operations which are written in sub class. That way you can build many other sub classes as per different requirements.

We have one trip planner and he has multiple packages of three days trips. Now how he create the itinerary for the customers. Suppose customer wants to go south or north then according to that planner will give him the package. Now lets see how he plans/executes without doing duplicate work. Planner has already one base format where he needs to mention how traveler will travel to destination, what they will do there on day 1,2 and 3 and at last how they will travel to back home.

Let’s write base class ThreeDaysTrip which already knows how to execute the trip.

By extending this base class, we are going to write two more class with different locations and different itinerary which will be used to execute two different trips.

In above code, You can see two classes NorthTrip and SouthTrip both are three days trip but process is very much similar of executing the whole trip. So all the abstract methods are overridden as per need of both the trips. This way if you are planning to start new trip in different direction then you need to write one more class and just add the itinerary.

Now let’s look at the planners code how that will use above classes.

As you can see, planner is asking for the place where customer wants to go and according to that he calls the different class. If custom wants to go south side then SouthTrip class is used. But if trip is not available the he straight forward will say sorry. :)

Certainly there are benefits of this pattern so let’s look at the advantages.

Advantages

  • Reduction of code duplication
  • As it uses inheritance, reusability of the code increases
  • Steps execution of the process can be customized

When you see advantages of anything, there are always none to minor disadvantages along with it.

Disadvantages

  • As process execution is hold by base class, with more and more changes in the program maintainability becomes difficult.
  • Sometimes, debugging becomes hard as execution is always happen in base class so writing better error handling can help.

I hope you have understood the pattern and its purpose. Thanks!

Source code — https://github.com/aarav-tech/design-patterns-python

Originally published at http://www.aaravtech.com.