|
31 | 31 | """
|
32 | 32 |
|
33 | 33 | import random
|
| 34 | +from typing import Type |
34 | 35 |
|
35 | 36 |
|
36 |
| -class PetShop: |
| 37 | +class Pet: |
| 38 | + def __init__(self, name: str) -> None: |
| 39 | + self.name = name |
37 | 40 |
|
38 |
| - """A pet shop""" |
| 41 | + def speak(self) -> None: |
| 42 | + raise NotImplementedError |
39 | 43 |
|
40 |
| - def __init__(self, animal_factory=None): |
41 |
| - """pet_factory is our abstract factory. We can set it at will.""" |
| 44 | + def __str__(self) -> str: |
| 45 | + raise NotImplementedError |
42 | 46 |
|
43 |
| - self.pet_factory = animal_factory |
44 | 47 |
|
45 |
| - def show_pet(self): |
46 |
| - """Creates and shows a pet using the abstract factory""" |
| 48 | +class Dog(Pet): |
| 49 | + def speak(self) -> None: |
| 50 | + print("woof") |
| 51 | + |
| 52 | + def __str__(self) -> str: |
| 53 | + return f"Dog<{self.name}>" |
| 54 | + |
47 | 55 |
|
48 |
| - pet = self.pet_factory() |
49 |
| - print(f"We have a lovely {pet}") |
50 |
| - print(f"It says {pet.speak()}") |
| 56 | +class Cat(Pet): |
| 57 | + def speak(self) -> None: |
| 58 | + print("meow") |
51 | 59 |
|
| 60 | + def __str__(self) -> str: |
| 61 | + return f"Cat<{self.name}>" |
52 | 62 |
|
53 |
| -class Dog: |
54 |
| - def speak(self): |
55 |
| - return "woof" |
56 | 63 |
|
57 |
| - def __str__(self): |
58 |
| - return "Dog" |
| 64 | +class PetShop: |
| 65 | + |
| 66 | + """A pet shop""" |
59 | 67 |
|
| 68 | + def __init__(self, animal_factory: Type[Pet]) -> None: |
| 69 | + """pet_factory is our abstract factory. We can set it at will.""" |
60 | 70 |
|
61 |
| -class Cat: |
62 |
| - def speak(self): |
63 |
| - return "meow" |
| 71 | + self.pet_factory = animal_factory |
| 72 | + |
| 73 | + def buy_pet(self, name: str) -> Pet: |
| 74 | + """Creates and shows a pet using the abstract factory""" |
64 | 75 |
|
65 |
| - def __str__(self): |
66 |
| - return "Cat" |
| 76 | + pet = self.pet_factory(name) |
| 77 | + print(f"Here is your lovely {pet}") |
| 78 | + return pet |
67 | 79 |
|
68 | 80 |
|
69 | 81 | # Additional factories:
|
70 | 82 |
|
71 | 83 | # Create a random animal
|
72 |
| -def random_animal(): |
| 84 | +def random_animal(name: str) -> Pet: |
73 | 85 | """Let's be dynamic!"""
|
74 |
| - return random.choice([Dog, Cat])() |
| 86 | + return random.choice([Dog, Cat])(name) |
75 | 87 |
|
76 | 88 |
|
77 | 89 | # Show pets with various factories
|
78 |
| -def main(): |
| 90 | +def main() -> None: |
79 | 91 | """
|
80 | 92 | # A Shop that sells only cats
|
81 | 93 | >>> cat_shop = PetShop(Cat)
|
82 |
| - >>> cat_shop.show_pet() |
83 |
| - We have a lovely Cat |
84 |
| - It says meow |
| 94 | + >>> pet = cat_shop.buy_pet("Lucy") |
| 95 | + Here is your lovely Cat<Lucy> |
| 96 | + >>> pet.speak() |
| 97 | + meow |
85 | 98 |
|
86 | 99 | # A shop that sells random animals
|
87 | 100 | >>> shop = PetShop(random_animal)
|
88 |
| - >>> for i in range(3): |
89 |
| - ... shop.show_pet() |
| 101 | + >>> for name in ["Max", "Jack", "Buddy"]: |
| 102 | + ... pet = shop.buy_pet(name) |
| 103 | + ... pet.speak() |
90 | 104 | ... print("=" * 20)
|
91 |
| - We have a lovely Cat |
92 |
| - It says meow |
| 105 | + Here is your lovely Cat<Max> |
| 106 | + meow |
93 | 107 | ====================
|
94 |
| - We have a lovely Dog |
95 |
| - It says woof |
| 108 | + Here is your lovely Dog<Jack> |
| 109 | + woof |
96 | 110 | ====================
|
97 |
| - We have a lovely Dog |
98 |
| - It says woof |
| 111 | + Here is your lovely Dog<Buddy> |
| 112 | + woof |
99 | 113 | ====================
|
100 | 114 | """
|
101 | 115 |
|
|
0 commit comments