Description
Defines the skeleton of an algorithm in an operation, and delays the step to the subclass. The template method allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
Schema Structure
Program example
Note: use the same set of tea and coffee
Code:
class Beverage(object): """description of class""" def makeBeverage(self): self.boilWater() self.brew() self.pourInCup() self.addCondiments() def boilWater(self): print ‘boil water‘ def brew(self): print ‘brew‘ def pourInCup(self): print ‘pour into cup‘ def addCondiments(self): print ‘add condiments‘class Coffee(Beverage): def brew(self): print ‘coffee‘ def addCondiments(self): print ‘Adding Sugar and Milk...‘class Tea(Beverage): def brew(self): print "tea" def addCondiments(self): print ‘Adding Lemon...‘if ‘__main__‘==__name__: coffee = Coffee() coffee.makeBeverage() tea = Tea() tea.makeBeverage()
Running result:
Reference Source:
Http://www.cnblogs.com/chenssy/p/3679190.html
Http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html
Http://www.cnblogs.com/zhuxiongfeng/archive/2010/04/09/1708615.html
Template Method Mode