-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (34 loc) · 1.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Source: https://refactoring.guru/design-patterns/factory-method/python/example
from abc import ABC, abstractmethod
class Product(ABC):
@abstractmethod
def operation(self) -> str:
pass
class ConcreteProductA(Product):
def operation(self) -> str:
return "{Result of the ConcreteProduct1}"
class ConcreteProductB(Product):
def operation(self) -> str:
return "{Result of the ConcreteProduct2}"
class Creator(ABC):
@abstractmethod
def factory_method(self):
pass
def some_operation(self) -> str:
product = self.factory_method()
result = f"Creator: The same creator's code has just worked with {product.operation()}"
return result
class ConcreteCreatorA(Creator):
def factory_method(self) -> Product:
return ConcreteProductA()
class ConcreteCreatorB(Creator):
def factory_method(self) -> Product:
return ConcreteProductB()
def client_code(creator: Creator) -> None:
print(f"Client: I'm not aware of the creator's class, but it still works. \n {creator.some_operation()}", end="")
if __name__ == "__main__":
print("App: Launched with the ConcreteCreator1.")
client_code(ConcreteCreatorA())
print("\n")
print("App: Launched with the ConcreteCreator2.")
client_code(ConcreteCreatorB())