Python模板模式详细操作教程
它定义了操作中算法的框架
它包括子类,这些子类重新定义了算法的某些步骤。
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
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()
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
Prepare Pizza
Cook Pizza
Eat Pizza
+++++++++++++++++++++++++
Prepare Tea
Cook Tea
Eat Tea