Skip to content

Commit 47c5043

Browse files
committed
chapter 9
1 parent 8a6198a commit 47c5043

File tree

7 files changed

+169
-9
lines changed

7 files changed

+169
-9
lines changed

README.md

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

iterator/transition/DinerMenu.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import Final, Iterator, List
2+
3+
from DinerMenuIterator import DinerMenuIterator
4+
from Menu import Menu
5+
from MenuItem import MenuItem
6+
7+
8+
class DinerMenu(Menu):
9+
MAX_ITEMS: Final[int] = 6
10+
numberOfItems: int = 0
11+
menuItems: List[MenuItem]
12+
13+
def __init__(self):
14+
self.menuItems = []
15+
16+
self.addItem("Vegetarian BLT","(Fakin') Bacon with lettuce & tomato on whole wheat", True, 2.99)
17+
self.addItem("BLT","Bacon with lettuce & tomato on whole wheat", False, 2.99)
18+
self.addItem("Soup of the day","Soup of the day, with a side of potato salad", False, 3.29)
19+
self.addItem("Hotdog","A hot dog, with sauerkraut, relish, onions, topped with cheese",False, 3.05)
20+
self.addItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice", True, 3.99)
21+
self.addItem("Pasta","Spaghetti with Marinara Sauce, and a slice of sourdough bread",True, 3.89)
22+
23+
def addItem(self, name: str, description: str, vegetarian: bool, price: float) -> None:
24+
menuItem: MenuItem = MenuItem(name, description, vegetarian, price)
25+
if self.numberOfItems >= self.MAX_ITEMS:
26+
print("Sorry, menu is full! Can't add item to menu")
27+
else:
28+
self.menuItems.append(menuItem)
29+
self.numberOfItems += 1
30+
31+
def getMenuItems(self) -> List[MenuItem]:
32+
return self.menuItems
33+
34+
def createIterator(self) -> Iterator[MenuItem]:
35+
return DinerMenuIterator(self.menuItems)
36+
# return AlternatingDinerMenuIterator(menuItems)
37+
38+
# other menu methods here
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import Iterator, List
2+
3+
from typing import Optional
4+
5+
from MenuItem import MenuItem
6+
7+
8+
class IllegalStateException(Exception): pass
9+
10+
class DinerMenuIterator(Iterator[MenuItem]):
11+
list: List[MenuItem]
12+
position: int = 0
13+
14+
def __init__(self, list: List[MenuItem]):
15+
self.list = list
16+
17+
def __next__(self) -> Optional[MenuItem]:
18+
if self.hasNext():
19+
menuItem: MenuItem = self.list[self.position]
20+
self.position += 1
21+
return menuItem
22+
else:
23+
raise StopIteration
24+
25+
def hasNext(self) -> bool:
26+
if self.position >= len(self.list) or self.list[self.position] == None:
27+
return False
28+
else:
29+
return True
30+
31+
def remove(self) -> None:
32+
if self.position <= 0:
33+
raise IllegalStateException("You can't remove an item until you've done at least one next()")
34+
if self.list[self.position-1] != None:
35+
for i in range(self.position-1, len(self.list)-1):
36+
self.list[i] = self.list[i+1]
37+
self.list[len(self.list)-1] = None

iterator/transition/Menu.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typing import Any, Iterator
2+
3+
4+
class Menu:
5+
def createIterator(self) -> Iterator[Any]:
6+
pass

iterator/transition/MenuItem.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MenuItem:
2+
name: str
3+
description: str
4+
vegetarian: bool
5+
price: float
6+
7+
def __init__(self, name: str, description: str, vegetarian: bool, price: float):
8+
self.name = name
9+
self.description = description
10+
self.vegetarian = vegetarian
11+
self.price = price
12+
13+
def getName(self) -> str:
14+
return self.name
15+
16+
def getDescription(self) -> str:
17+
return self.description
18+
19+
def getPrice(self) -> float:
20+
return self.price
21+
22+
def isVegetarian(self) -> bool:
23+
return self.vegetarian
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import Iterator, List
2+
3+
from Menu import Menu
4+
from MenuItem import MenuItem
5+
6+
7+
class PancakeHouseMenu(Menu):
8+
menuItems: List[MenuItem]
9+
10+
def __init__(self):
11+
self.menuItems = []
12+
13+
self.addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs and toast", True,2.99)
14+
self.addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", False,2.99)
15+
self.addItem("Blueberry Pancakes","Pancakes made with fresh blueberries and blueberry syrup",True,3.49)
16+
self.addItem("Waffles","Waffles with your choice of blueberries or strawberries",True,3.59)
17+
18+
def addItem(self, name: str, description: str, vegetarian: bool, price: float) -> None:
19+
menuItem: MenuItem = MenuItem(name, description, vegetarian, price)
20+
self.menuItems.append(menuItem)
21+
22+
def getMenuItems(self) -> List[MenuItem]:
23+
return self.menuItems
24+
25+
def createIterator(self) -> Iterator[MenuItem]:
26+
return iter(self.menuItems)
27+
28+
# other menu methods here

iterator/transition/Waitress.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Any, Iterator, List
2+
3+
from Menu import Menu
4+
from MenuItem import MenuItem
5+
6+
7+
class Waitress:
8+
menus: List[Menu]
9+
10+
def __init__(self, menus: List[Menu]):
11+
self.menus = menus
12+
13+
def printMenu(self) -> None:
14+
menuIterator: Iterator[Any] = iter(self.menus)
15+
menu = next(menuIterator, None) # not all "menu" has the method "hasNext".
16+
while menu != None:
17+
self.printMenu_(menu.createIterator())
18+
menu = next(menuIterator, None)
19+
20+
# authors' java code has two "printMenu", which cannot be translated to python.
21+
def printMenu_(self, iterator: Iterator[Any]) -> None:
22+
menuItem = next(iterator, None) # not all "iterator" has the method "hasNext".
23+
while menuItem != None:
24+
print(f"{menuItem.getName()}, ", end="")
25+
print(f"{menuItem.getPrice()} -- ", end="")
26+
print(menuItem.getDescription())
27+
menuItem = next(iterator, None)

0 commit comments

Comments
 (0)