Skip to content

Commit 8a6198a

Browse files
committed
chapter 8
1 parent 38b123b commit 8a6198a

17 files changed

+337
-7
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Python Head First Design Patterns
22
## Chapters
3-
[chapter 1](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/strategy)-done
4-
[chapter 2](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/observer)-done (Only swing has no translation.)
5-
[chapter 3](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/decorator)-done (Only io has no translation.)
6-
[chapter 4](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/factory)-done
7-
[chapter 5](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/singleton)-done
8-
[chapter 6](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/command)-done (Only swing has no translation.)
3+
[chapter 1(strategy)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/strategy)-done
4+
[chapter 2(observer)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/observer)-done (Only swing has no translation.)
5+
[chapter 3(decorator)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/decorator)-done (Only io has no translation.)
6+
[chapter 4(factory)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/factory)-done
7+
[chapter 5(singleton)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/singleton)-done
8+
[chapter 6(command)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/command)-done (Only swing has no translation.)
99
[chapter 7(adapter)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/adapter)-done
1010
[chapter 7(facade)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/facade/hometheater)-done
11-
11+
[chapter 8(templatemethod)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/templatemethod)-done (applet and frame have no translation.)
1212
## Introduction
1313
1. Translated from [Head-First-Design-Patterns](https://github.com/bethrobson/Head-First-Design-Patterns) (java)
1414
2. What are the differences between other Python version Head-First-Design-Patterns and ours?

templatemethod/applet/I do not know how to translate.txt

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from Coffee import Coffee
2+
from CoffeeWithHook import CoffeeWithHook
3+
from Tea import Tea
4+
from TeaWithHook import TeaWithHook
5+
6+
7+
class BeverageTestDrive:
8+
@staticmethod
9+
def main(*args):
10+
tea: Tea = Tea()
11+
coffee: Coffee = Coffee()
12+
13+
print("\nMaking tea...")
14+
tea.prepareRecipe()
15+
16+
print("\nMaking coffee...")
17+
coffee.prepareRecipe()
18+
19+
teaHook: TeaWithHook = TeaWithHook()
20+
coffeeHook: CoffeeWithHook = CoffeeWithHook()
21+
22+
print("\nMaking tea...")
23+
teaHook.prepareRecipe()
24+
25+
print("\nMaking coffee...")
26+
coffeeHook.prepareRecipe()
27+
28+
if __name__ == "__main__":
29+
BeverageTestDrive.main()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class CaffeineBeverage(ABC):
5+
6+
def prepareRecipe(self) -> None:
7+
self.boilWater()
8+
self.brew()
9+
self.pourInCup()
10+
self.addCondiments()
11+
12+
@abstractmethod
13+
def brew(self) -> None:
14+
pass
15+
16+
@abstractmethod
17+
def addCondiments(self) -> None:
18+
pass
19+
20+
def boilWater(self) -> None:
21+
print("Boiling water")
22+
23+
def pourInCup(self) -> None:
24+
print("Pouring into cup")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class CaffeineBeverageWithHook(ABC):
5+
6+
def prepareRecipe(self) -> None:
7+
self.boilWater()
8+
self.brew()
9+
self.pourInCup()
10+
if self.customerWantsCondiments():
11+
self.addCondiments()
12+
13+
@abstractmethod
14+
def brew(self) -> None:
15+
pass
16+
17+
@abstractmethod
18+
def addCondiments(self) -> None:
19+
pass
20+
21+
def boilWater(self) -> None:
22+
print("Boiling water")
23+
24+
def pourInCup(self) -> None:
25+
print("Pouring into cup")
26+
27+
def customerWantsCondiments(self) -> bool:
28+
return True

templatemethod/barista/Coffee.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from CaffeineBeverage import CaffeineBeverage
2+
3+
4+
class Coffee(CaffeineBeverage):
5+
def brew(self) -> None:
6+
print("Dripping Coffee through filter")
7+
8+
def addCondiments(self) -> None:
9+
print("Adding Sugar and Milk")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from CaffeineBeverageWithHook import CaffeineBeverageWithHook
2+
3+
4+
class CoffeeWithHook(CaffeineBeverageWithHook):
5+
6+
def brew(self) -> None:
7+
print("Dripping Coffee through filter")
8+
9+
def addCondiments(self) -> None:
10+
print("Adding Sugar and Milk")
11+
12+
def customerWantsCondiments(self) -> bool:
13+
answer: str = self.__getUserInput()
14+
if answer.lower().startswith('y'):
15+
return True
16+
else:
17+
return False
18+
19+
def __getUserInput(self) -> str:
20+
answer = None
21+
print("Would you like milk and sugar with your coffee (y/n)? ")
22+
try:
23+
answer = input()
24+
except IOError:
25+
print("IO error trying to read your answer")
26+
if answer == None:
27+
return 'no'
28+
return answer

templatemethod/barista/Tea.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from CaffeineBeverage import CaffeineBeverage
2+
3+
4+
class Tea(CaffeineBeverage):
5+
def brew(self) -> None:
6+
print("Steeping the tea")
7+
def addCondiments(self) -> None:
8+
print("Adding Lemon")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from CaffeineBeverageWithHook import CaffeineBeverageWithHook
2+
3+
4+
class TeaWithHook(CaffeineBeverageWithHook):
5+
6+
def brew(self) -> None:
7+
print("Steeping the tea")
8+
9+
def addCondiments(self) -> None:
10+
print("Adding Lemon")
11+
12+
def customerWantsCondiments(self) -> bool:
13+
answer: str = self.__getUserInput()
14+
15+
if answer.lower().startswith('y'):
16+
return True
17+
else:
18+
return False
19+
20+
def __getUserInput(self) -> str:
21+
answer: str = None
22+
print("Would you like lemon with your tea (y/n)? ")
23+
try:
24+
answer = input()
25+
except IOError:
26+
print("IO error trying to read your answer")
27+
if answer == None:
28+
return "no"
29+
return answer

templatemethod/frame/I dont know how to use python GUI library.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)