-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_main.py
More file actions
89 lines (60 loc) · 2.42 KB
/
abstract_main.py
File metadata and controls
89 lines (60 loc) · 2.42 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import annotations
from abc import ABC, abstractmethod
class AbstractFactory(ABC):
@abstractmethod
def create_product_a(self) -> AbstractProductA:
pass
@abstractmethod
def create_product_b(self) -> AbstractProductB:
pass
class ConcreteFactory1(AbstractFactory):
def create_product_a(self) -> AbstractProductA:
return ConcreteProductA1()
def create_product_b(self) -> AbstractProductB:
return ConcreteProductB1()
class ConcreteFactory2(AbstractFactory):
def create_product_a(self) -> AbstractProductA:
return ConcreteProductA2()
def create_product_b(self) -> AbstractProductB:
return ConcreteProductB2()
class AbstractProductA(ABC):
@abstractmethod
def useful_function_a(self) -> str:
pass
class ConcreteProductA1(AbstractProductA):
def useful_function_a(self) -> str:
return "This is concretion of product A1"
class ConcreteProductA2(AbstractProductA):
def useful_function_a(self) -> str:
return "This is concretion of product A2"
class AbstractProductB(ABC):
@abstractmethod
def useful_function_b(self) -> None:
pass
@abstractmethod
def another_useful_function_b(self, collaborator: AbstractProductA) -> None:
pass
class ConcreteProductB1(AbstractProductB):
def useful_function_b(self) -> str:
return "This is concretion of product B1"
def another_useful_function_b(self, collaborator: AbstractProductA) -> str:
result = collaborator.useful_function_a()
return f"The result of B1 collaborating with ({result})"
class ConcreteProductB2(AbstractProductB):
def useful_function_b(self) -> str:
return "This is concretion of product B2"
def another_useful_function_b(self, collaborator: AbstractProductA) -> str:
result = collaborator.useful_function_a()
return f"The result of B2 collaborating with ({result})"
def client_code(factory: AbstractFactory) -> None:
product_a = factory.create_product_a()
product_b = factory.create_product_b()
print(f"{product_b.useful_function_b()}")
print(f"{product_b.another_useful_function_b(product_a)}")
print("\n")
if __name__ == "__main__":
print("Client : Testing code with first factory type:")
client_code(ConcreteFactory1())
print("\n")
print("Client : Testing the same code with second factory type:")
client_code(ConcreteFactory2())