-
Notifications
You must be signed in to change notification settings - Fork 3
/
template_method.py
62 lines (43 loc) · 1.15 KB
/
template_method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from abc import abstractmethod
class MealTemplate:
def __init__(self, name):
self.name = name
def go(self):
self.prepare()
self.cook()
self.eat()
@abstractmethod
def prepare(self):
pass
@abstractmethod
def cook(self):
pass
@abstractmethod
def eat(self):
pass
class Pizza(MealTemplate):
def __init__(self, name, ingredient):
super().__init__(name)
self.ingredient = ingredient
def prepare(self):
print(f"preparing {self.name}...")
print(f"adding {self.ingredient}")
def cook(self):
print(f"cooking {self.name}...")
def eat(self):
print(f"eating {self.name}...")
class Tea(MealTemplate):
def __init__(self, name, temperature):
super().__init__(name)
self.temperature = temperature
def prepare(self):
print(f"preparing {self.name}...")
def cook(self):
print(f"cooking {self.name} under {self.temperature}ºC...")
def eat(self):
print(f"drinking {self.name}...")
pizza = Pizza("my pizza", "cheese")
pizza.go()
print()
tea = Tea("black tea", 80)
tea.go()