0% found this document useful (0 votes)
16 views8 pages

Template Design Pattern

Uploaded by

Om Bhayde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Template Design Pattern

Uploaded by

Om Bhayde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

MADHAV INSTITUTE OF TECHNOLOGY & SCIENCE, GWALIOR

Deemed to be University
(Declared Under Distinct Category by Ministry of Education, Government of
India)
NAAC Accredited with A++ grades

Template method design


pattern

SUBMITTED
SUBMITTED
BY :
TO :
Devesh Kumar Lal Om Bhayde -
0901CD221042
Introduction to Design
Patterns

• Definition: Design patterns are reusable solutions to common


problems in software design.

• Purpose: They provide best practices and guide developers in


designing flexible and maintainable code

Page 2
What is Template Method
Pattern?
• Definition: A behavioral design pattern that defines the
skeleton of an algorithm in a method, allowing subclasses to
redefine certain steps without changing the algorithm's
structure.

• Key Idea: It provides a base method (template) where steps


can be customized by subclasses.
Template Method Pattern
Structure
• Abstract Class: Contains the template method that defines the overall
workflow.
• Concrete Classes: Implement the customizable steps of the algorithm.

• Abstract Class (Template Method):


⚬ Defines the overall structure of an algorithm (template method).
⚬ The algorithm consists of multiple steps (methods).
• Method 1 (Step 1):
⚬ A fixed step in the template method.
⚬ Defined in the base class and not meant to be overridden.
• Method 2 (Step 2 - customizable by subclass):
⚬ A customizable step defined as an abstract or virtual method.
⚬ Each subclass provides its own implementation of this step.
• Method 3 (Step 3):
⚬ Another fixed step defined in the base class.
⚬ Common across all subclasses and typically not overridden.
• Concrete Class 1:
⚬ Implements the template method with its own logic for Step 2.
• Concrete Class 2:
⚬ Implements the template method with a different version of
from abc import ABC, abstractmethod

# Abstract Class
CODE def play_turn(self):
print("Playing Chess Turn")

class Game(ABC): def end(self):


# Template Method print("Ending Chess Game")
def play(self):
self.start() # Concrete Class 2: Football
self.play_turn() class Football(Game):
self.end() def start(self):
print("Starting Football Game")
@abstractmethod
def start(self): def play_turn(self):
pass print("Playing Football Turn")

@abstractmethod def end(self):


def play_turn(self): print("Ending Football Game")
pass
# Example usage
@abstractmethod if __name__ == "__main__":
def end(self): chess = Chess()
pass chess.play()

# Concrete Class 1: Chess football = Football()


class Chess(Game): football.play()
def start(self):
print("Starting Chess Game")
Real-World
Example
• Scenario: Cooking a meal
• Template Method: Recipe (Step-by-step guide)
• Concrete Classes: Different dishes (custom steps based on the type of
meal)
• Steps:
1.Prepare Ingredients
2.Cook (customizable)
3.Serve
• Each dish (subclass) implements its own version of the cooking step.

Advantages of Template Method


• Pattern
Promotes code reuse by defining the common
structure.
• Enforces a consistent workflow in the algorithm.
• Helps avoid duplicate code in subclasses.
Conclusio
n
• The Template Method Pattern helps in defining a framework for an
algorithm with customizable steps.
• It promotes flexibility and reuse by allowing subclasses to override
certain parts of the workflow.

Key Takeaway: Use the Template Method Pattern when you want to
standardize the overall structure but allow certain steps to vary in
subclasses.

You might also like