Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
__author__ = 'Raja Ramanathan'
__doc__ = "Template design pattern in python"
import sys
class AbstractBurrito:
'Abstract class for making a burrito'
"""
Constructor
"""
def __init__(self):
self.meat = None
def __heatTortilla(self):
print 'Warming tortilla'
def __addBeans(self):
print 'Adding beans...'
def __addSalsa(self):
print 'Adding salsa'
def __addCorns(self):
print 'Adding corns...'
"""
For the demo the assumption is that the burritos vary ONLY by the meat in it.
So this method calls the template method on the child class.
Note: The template method on the child class should be non-private.
"""
def __addMeat(self):
self.selectMeat()
print 'Adding %s'%self.meat
"""
Steps to prepare a burrito
"""
def make(self):
self.__heatTortilla()
self.__addBeans()
self.__addMeat()
self.__addSalsa()
self.__addCorns()
print 'Your %s burrito is warm and ready'%self.meat
print 'Thank You!!!'
class ChickenBurrito(AbstractBurrito):
"""
Chicken burrito
"""
def selectMeat(self):
self.meat = 'Chicken'
class BeefBurrito(AbstractBurrito):
"""
Beef burrito
"""
def selectMeat(self):
self.meat = 'Beef'
def main():
myburrito = ChickenBurrito()
myburrito.make()
urburrito = BeefBurrito()
urburrito.make()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.