Skip to content

Commit 38b123b

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

File tree

9 files changed

+97
-1
lines changed

9 files changed

+97
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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 (Only challenge has no translation.)
3+
[chapter 1](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/strategy)-done
44
[chapter 2](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/observer)-done (Only swing has no translation.)
55
[chapter 3](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/decorator)-done (Only io has no translation.)
66
[chapter 4](https://github.com/rebuild-123/Python-Head-First-Design-Patterns/tree/main/factory)-done
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from PhoneCameraApp import PhoneCameraApp
2+
3+
4+
class BasicCameraApp(PhoneCameraApp):
5+
def edit(self) -> None:
6+
print("Basic editing features")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from PhoneCameraApp import PhoneCameraApp
2+
3+
4+
class CameraPlusApp(PhoneCameraApp):
5+
def edit(self) -> None:
6+
print("Extra snazzy photo editing features")

strategy/challenge/Email.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from ShareStrategy import ShareStrategy
2+
3+
4+
class Email(ShareStrategy):
5+
def share(self) -> None:
6+
print("I'm emailing the photo")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from abc import ABC, abstractmethod
2+
3+
from ShareStrategy import ShareStrategy
4+
5+
6+
class PhoneCameraApp(ABC):
7+
shareStrategy: ShareStrategy
8+
9+
def setShareStrategy(self, shareStrategy: ShareStrategy) -> None:
10+
self.shareStrategy = shareStrategy
11+
12+
def share(self) -> None:
13+
self.shareStrategy.share()
14+
15+
def take(self) -> None:
16+
print("Taking the photo")
17+
18+
def save(self) -> None:
19+
print("Saving the photo")
20+
21+
@abstractmethod
22+
def edit(self) -> None:
23+
pass
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import re
2+
import sys
3+
4+
from BasicCameraApp import BasicCameraApp
5+
from Email import Email
6+
from PhoneCameraApp import PhoneCameraApp
7+
from Social import Social
8+
from Txt import Txt
9+
10+
11+
class PhotoWithPhone:
12+
@staticmethod
13+
def main(*args):
14+
cameraApp: PhoneCameraApp = BasicCameraApp()
15+
share: str = PhotoWithPhone.getSharing()
16+
if re.match(r'3\.[1-9][0-9]\.', sys.version[:5]):
17+
# Only for python 3.10 and later
18+
match share:
19+
case "t": cameraApp.setShareStrategy(Txt())
20+
case "e": cameraApp.setShareStrategy(Email())
21+
case "s": cameraApp.setShareStrategy(Social())
22+
case _: cameraApp.setShareStrategy(Txt())
23+
else:
24+
if share == "t": cameraApp.setShareStrategy(Txt())
25+
elif share == "e": cameraApp.setShareStrategy(Email())
26+
elif share == "s": cameraApp.setShareStrategy(Social())
27+
else: cameraApp.setShareStrategy(Txt())
28+
cameraApp.take()
29+
cameraApp.edit()
30+
cameraApp.save()
31+
cameraApp.share()
32+
33+
@staticmethod
34+
def getSharing() -> str:
35+
print("Share with txt (t), email (e), or social media (s)?")
36+
appName = input()
37+
return appName
38+
39+
if __name__ == "__main__":
40+
PhotoWithPhone.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ShareStrategy:
2+
def share(self) -> None:
3+
pass

strategy/challenge/Social.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from ShareStrategy import ShareStrategy
2+
3+
4+
class Social(ShareStrategy):
5+
def share(self) -> None:
6+
print("I'm posting the photo on social media")

strategy/challenge/Txt.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from ShareStrategy import ShareStrategy
2+
3+
4+
class Txt(ShareStrategy):
5+
def share(self) -> None:
6+
print("I'm txting the photo")

0 commit comments

Comments
 (0)