Skip to content

Commit b86fe10

Browse files
committed
feat: Add Catory Method pattern with Animal sound example
1 parent 88025ae commit b86fe10

File tree

1 file changed

+47
-0
lines changed
  • Design-Patterns/Creational/Factory Method/Parsa-Soroush

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from abc import ABC, abstractmethod
2+
3+
class Animal(ABC):
4+
@abstractmethod
5+
def make_sound(self):
6+
pass
7+
8+
9+
class Dog(Animal):
10+
def make_sound(self):
11+
print("Dog: Woof woof!")
12+
13+
14+
class Cat(Animal):
15+
def make_sound(self):
16+
print("Cat: Meow meow!")
17+
18+
19+
class AnimalTrainer(ABC):
20+
21+
@abstractmethod
22+
def make_animal(self) -> Animal:
23+
pass
24+
25+
def teach_to_speak(self):
26+
animal = self.make_animal()
27+
animal.make_sound()
28+
29+
30+
class DogTrainer(AnimalTrainer):
31+
def make_animal(self) -> Animal:
32+
return Dog()
33+
34+
35+
class CatTrainer(AnimalTrainer):
36+
def make_animal(self) -> Animal:
37+
return Cat()
38+
39+
40+
if __name__ == "__main__":
41+
print("Dog Trainer:")
42+
dog_trainer = DogTrainer()
43+
dog_trainer.teach_to_speak()
44+
45+
print("\nCat Trainer:")
46+
cat_trainer = CatTrainer()
47+
cat_trainer.teach_to_speak()

0 commit comments

Comments
 (0)