Skip to content

Commit c8c15c1

Browse files
committed
Appendix
1 parent b823e19 commit c8c15c1

33 files changed

+962
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ chapter 13 (No code)
2020
[Appendix (collections)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/collections)
2121
[Appendix (ducks)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/ducks)
2222
[Appendix (iterenum)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/iterenum)
23+
[Appendix (bridge)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/bridge)
24+
[Appendix (builder)](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/builder)
2325

2426
## Introduction
2527
1. Translated from [Head-First-Design-Patterns](https://github.com/bethrobson/Head-First-Design-Patterns) (java)

bridge/remote/Client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from GenericRemote import GenericRemote
2+
from SpecialRemote import SpecialRemote
3+
from TVFactory import TVFactory
4+
5+
6+
class Client:
7+
@staticmethod
8+
def main(*args) -> None:
9+
tvFactory: TVFactory = TVFactory()
10+
remoteSony: SpecialRemote = SpecialRemote(tvFactory)
11+
print("Connect your remote to the TV")
12+
remoteSony.setTV("Sony")
13+
remoteSony.on()
14+
remoteSony.up()
15+
remoteSony.down()
16+
remoteSony.off()
17+
18+
remoteLG: GenericRemote = GenericRemote(tvFactory)
19+
print("Connect your remote to the TV")
20+
remoteLG.setTV("LG")
21+
remoteLG.on()
22+
remoteLG.nextChannel()
23+
remoteLG.prevChannel()
24+
remoteLG.off()
25+
26+
if __name__ == "__main__":
27+
Client.main()

bridge/remote/GenericRemote.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from RemoteControl import RemoteControl
2+
from TVFactory import TVFactory
3+
4+
5+
class GenericRemote(RemoteControl):
6+
def __init__(self, tvFactory: TVFactory):
7+
super().__init__(tvFactory)
8+
def nextChannel(self) -> None:
9+
channel: int = self.getChannel()
10+
self.setChannel(channel+1)
11+
def prevChannel(self) -> None:
12+
channel: int = self.getChannel()
13+
self.setChannel(channel-1)

bridge/remote/LG.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from TV import TV
2+
3+
4+
class LG(TV):
5+
channel: int = 1
6+
def on(self) -> None:
7+
print("Turning on the LG TV")
8+
def off(self) -> None:
9+
print("Turning off the LG TV")
10+
def tuneChannel(self, channel: int) -> None:
11+
self.channel = channel
12+
print(f"Set the LG TV Channel to {self.channel}")
13+
def getChannel(self) -> int:
14+
return self.channel

bridge/remote/RemoteControl.py

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+
from TV import TV
4+
from TVFactory import TVFactory
5+
6+
7+
class RemoteControl(ABC):
8+
tv: TV
9+
tvFactory: TVFactory
10+
def __init__(self, tvFactory: TVFactory):
11+
self.tvFactory = tvFactory
12+
def on(self) -> None:
13+
self.tv.on()
14+
def off(self) -> None:
15+
self.tv.off()
16+
def setChannel(self, channel: int) -> None:
17+
self.tv.tuneChannel(channel)
18+
def getChannel(self) -> int:
19+
return self.tv.getChannel()
20+
def setTV(self, type_: str) -> None:
21+
try:
22+
self.tv = self.tvFactory.getTV(type_)
23+
except Exception as e:
24+
print(e)

bridge/remote/Sony.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from TV import TV
2+
3+
4+
class Sony(TV):
5+
station: int = 0
6+
def on(self) -> None:
7+
print("Turning on the Sony TV")
8+
def off(self) -> None:
9+
print("Turning off the Sony TV");
10+
def tuneChannel(self, channel: int) -> None:
11+
self.station = channel
12+
print(f"Set the Sony TV station to {self.station}")
13+
def getChannel(self) -> int:
14+
return self.station

bridge/remote/SpecialRemote.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from RemoteControl import RemoteControl
2+
from TVFactory import TVFactory
3+
4+
5+
class SpecialRemote(RemoteControl):
6+
def __init__(self, tvFactory: TVFactory):
7+
super().__init__(tvFactory)
8+
def up(self) -> None:
9+
channel: int = self.getChannel()
10+
self.setChannel(channel+1)
11+
def down(self) -> None:
12+
channel: int = self.getChannel()
13+
self.setChannel(channel-1)

bridge/remote/TV.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class TV(ABC):
5+
@abstractmethod
6+
def on(self) -> None:
7+
pass
8+
@abstractmethod
9+
def off(self) -> None:
10+
pass
11+
@abstractmethod
12+
def tuneChannel(self, channel: int) -> None:
13+
pass
14+
@abstractmethod
15+
def getChannel(self) -> int:
16+
pass

bridge/remote/TVFactory.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from LG import LG
2+
from Sony import Sony
3+
from TV import TV
4+
5+
6+
class TVFactory:
7+
def getTV(self, type_: str) -> TV:
8+
if type_ == 'LG':
9+
return LG()
10+
elif type_ == 'Sony':
11+
return Sony()
12+
else:
13+
raise Exception("Invalid TV Type")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from House import House
2+
from HouseBuilder import HouseBuilder
3+
from InteriorWall import InteriorWall
4+
from Roof import Roof
5+
from Wall import Wall
6+
from Window import Window
7+
8+
9+
class GingerbreadHouseBuilder(HouseBuilder):
10+
numWalls: int = 4
11+
numWindows: int = 4
12+
windowMaterial: str = "Sugar"
13+
wallMaterial: str = "Gingerbread and icing"
14+
roofMaterial: str = "Twizzlers"
15+
def __init__(self) -> None:
16+
self.builderName = "Gingerbread House Builder"
17+
self.setHouseType(self.HouseType.GINGERBREAD)
18+
self.house.name = "Gingerbread House" # Not in the authors' code.
19+
def addWalls(self) -> HouseBuilder:
20+
# add exterior walls
21+
for i in range(4):
22+
print(f"Adding wall made out of {self.wallMaterial}")
23+
self.house.addWall(Wall(self.wallMaterial))
24+
return self
25+
def addWindows(self) -> HouseBuilder:
26+
for i in range(self.numWindows):
27+
print(f"Adding window made out of {self.windowMaterial}")
28+
self.house.addWindow(Window(self.windowMaterial))
29+
return self
30+
def addRoof(self) -> HouseBuilder:
31+
self.house.addRoof(Roof(self.roofMaterial))
32+
return self
33+
def build(self) -> House:
34+
print("Stick everything together with icing")
35+
return self.house

0 commit comments

Comments
 (0)