Open In App

Template Method – Python Design Patterns

The Template method is a Behavioral Design Pattern that defines the skeleton of the operation and leaves the details to be implemented by the child class. Its subclasses can override the method implementations as per need but the invocation is to be in the same way as defined by an abstract class. It is one of the easiest among the Behavioral design pattern to understand and implements. Such methods are highly used in framework development as they allow us to reuse the single piece of code at different places by making certain changes. This leads to avoiding code duplication also.
 

Problem without using Template Method

Imagine you are working on a Chatbot application as a software developer which uses data mining techniques to analyze the data of the corporate documents. Initially, your applications were fine with the pdf version of the data only but later your applications also require to collect and convert data from other formats also such as XML, CSV, and others. After implementing the whole scenario for the other formats also, you noticed that all the classes have lots of similar code. Part of the code like analyzing and processing was identical in almost all classes whereas they differ in dealing with the data.
 



Problem-Template-Method

 

Solution using Template Method

Let’s discuss the solution to the above-described problem using the template method. It suggests to break down the code into a series of steps and convert these steps into methods and put series call inside the template_function. Hence we created the template_function separately and create methods such as get_xml, get_pdf and get_csv for dealing with the code separately.
 






""" method to get the text of file"""
def get_text():
     
    return "plain_text"
 
""" method to get the xml version of file"""
def get_xml():
     
    return "xml"
 
""" method to get the pdf version of file"""
def get_pdf():
     
    return "pdf"
 
"""method to get the csv version of file"""
def get_csv():
     
    return "csv"
 
"""method used to convert the data into text format"""
def convert_to_text(data):
     
    print("[CONVERT]")
    return "{} as text".format(data)
 
"""method used to save the data"""
def saver():
     
    print("[SAVE]")
 
"""helper function named as template_function"""
def template_function(getter, converter = False, to_save = False):
 
    """input data from getter"""
    data = getter()
    print("Got `{}`".format(data))
 
    if len(data) <= 3 and converter:
        data = converter(data)
    else:
        print("Skip conversion")
     
    """saves the data only if user want to save it"""
    if to_save:
        saver()
 
    print("`{}` was processed".format(data))
 
 
"""main method"""
if __name__ == "__main__":
 
    template_function(get_text, to_save = True)
 
    template_function(get_pdf, converter = convert_to_text)
 
    template_function(get_csv, to_save = True)
 
    template_function(get_xml, to_save = True)

Output

Got `plain_text`
Skip conversion
[SAVE]
`plain_text` was processed
Got `pdf`
[CONVERT]
`pdf as text` was processed
Got `csv`
Skip conversion
[SAVE]
`csv` was processed

Class Diagram

Following is the class diagram for the Template Method
 

Class-diagram-template-method

 

Advantages

 

 

Disadvantages

 

 

Applicability

 

Further Read – Template Method in Java
 


Article Tags :