File tree Expand file tree Collapse file tree 17 files changed +337
-7
lines changed
Expand file tree Collapse file tree 17 files changed +337
-7
lines changed Original file line number Diff line number Diff line change 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
13131 . Translated from [ Head-First-Design-Patterns] ( https://github.com/bethrobson/Head-First-Design-Patterns ) (java)
14142 . What are the differences between other Python version Head-First-Design-Patterns and ours?
Original file line number Diff line number Diff line change 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 ("\n Making tea..." )
14+ tea .prepareRecipe ()
15+
16+ print ("\n Making coffee..." )
17+ coffee .prepareRecipe ()
18+
19+ teaHook : TeaWithHook = TeaWithHook ()
20+ coffeeHook : CoffeeWithHook = CoffeeWithHook ()
21+
22+ print ("\n Making tea..." )
23+ teaHook .prepareRecipe ()
24+
25+ print ("\n Making coffee..." )
26+ coffeeHook .prepareRecipe ()
27+
28+ if __name__ == "__main__" :
29+ BeverageTestDrive .main ()
Original file line number Diff line number Diff line change 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" )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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" )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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" )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments