Skip to content

Commit 1f993bd

Browse files
committed
Implemented the basic template pattern
1 parent 3347705 commit 1f993bd

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

chapter08_template/barista.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class CaffeineBeverage:
2+
def prepare_recipe(self):
3+
self.boil_water()
4+
self.brew()
5+
self.pour_in_cup()
6+
self.add_condiments()
7+
8+
def brew(self):
9+
raise NotImplementedError
10+
11+
def add_condiments(self):
12+
raise NotImplementedError
13+
14+
def boil_water(self):
15+
print("Boiling water")
16+
17+
def pour_in_cup(self):
18+
print("Pouring into cup")
19+
20+
21+
class Tea(CaffeineBeverage):
22+
def brew(self):
23+
print("Steeping the tea")
24+
25+
def add_condiments(self):
26+
print("Adding Lemon")
27+
28+
29+
class Coffee(CaffeineBeverage):
30+
def brew(self):
31+
print("Dripping Coffee through filter")
32+
33+
def add_condiments(self):
34+
print("Adding Sugar and Milk")
35+
36+
37+
def beverage_test_drive():
38+
tea = Tea()
39+
coffee = Coffee()
40+
print("\nMaking tea...")
41+
tea.prepare_recipe()
42+
print("\nMaking coffee...")
43+
coffee.prepare_recipe()
44+
45+
46+
if __name__ == "__main__":
47+
beverage_test_drive()

chapter08_template/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Chapter 8: Template design pattern
22

3-
> **Template Method**: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
3+
> **Template Method**: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

0 commit comments

Comments
 (0)