Python Design Patterns Tutorial
Python Design Patterns Resources
Selected Reading
- Exception Handling
- Python Design Patterns - Anti
- Concurrency in Python
- Strings & Serialization
- Python Design Patterns - Queues
- Python Design Patterns - Sets
- Lists Data Structure
- Dictionaries
- Python Design Patterns - Iterator
- Object Oriented Concepts Implementation
- Object Oriented
- Abstract Factory
- Python Design Patterns - Flyweight
- Python Design Patterns - Template
- Python Design Patterns - Strategy
- Python Design Patterns - State
- Python Design Patterns - Observer
- Chain of Responsibility Pattern
- Python Design Patterns - Proxy
- Python Design Patterns - Decorator
- Python Design Patterns - Adapter
- Python Design Patterns - Command
- Python Design Patterns - Facade
- Python Design Patterns - Prototype
- Python Design Patterns - Builder
- Python Design Patterns - Factory
- Python Design Patterns - Singleton
- Model View Controller Pattern
- Python Design Patterns - Gist
- Introduction
- Python Design Patterns - Home
Python Design Patterns Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Design Patterns - Template
Python Design Patterns - Template
A template pattern defines a basic algorithm in a base class using abstract operation where subclasses override the concrete behavior. The template pattern keeps the outpne of algorithm in a separate method. This method is referred as the template method.
Following are the different features of the template pattern −
It defines the skeleton of algorithm in an operation
It includes subclasses, which redefine certain steps of an algorithm.
class MakeMeal: def prepare(self): pass def cook(self): pass def eat(self): pass def go(self): self.prepare() self.cook() self.eat() class MakePizza(MakeMeal): def prepare(self): print "Prepare Pizza" def cook(self): print "Cook Pizza" def eat(self): print "Eat Pizza" class MakeTea(MakeMeal): def prepare(self): print "Prepare Tea" def cook(self): print "Cook Tea" def eat(self): print "Eat Tea" makePizza = MakePizza() makePizza.go() print 25*"+" makeTea = MakeTea() makeTea.go()
Output
The above program generates the following output −
Explanation
This code creates a template to prepare meal. Here, each parameter represents the attribute to create a part of meal pke tea, pizza, etc.
The output represents the visuapzation of attributes.
Advertisements