Created
January 10, 2015 21:19
-
-
Save rajaramanathan/0a6b9e4bc98d022cf2da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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