-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_factory.py
More file actions
102 lines (67 loc) · 2.71 KB
/
abstract_factory.py
File metadata and controls
102 lines (67 loc) · 2.71 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
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import annotations
from abc import ABC, abstractmethod
class Shoes(ABC):
@abstractmethod
def create_adidas_shoes(self) -> AdidasShoes:
pass
@abstractmethod
def create_nike_shoes(self) -> NikeShoes:
pass
class RunningFactory(Shoes):
def create_adidas_shoes(self) -> AdidasShoes:
return AdidasRunningShoes()
def create_nike_shoes(self) -> NikeShoes:
return NikeRunningShoes()
class SkateboardingFactory(Shoes):
def create_adidas_shoes(self) -> AdidasShoes:
return AdidasSkateboardingShoes()
def create_nike_shoes(self) -> NikeShoes:
return NikeSkateboardingShoes()
class AdidasShoes(ABC):
@abstractmethod
def made_in_germany(self) -> str:
pass
"""
Concrete Products are created by corresponding Concrete Factories.
"""
class AdidasRunningShoes(AdidasShoes):
def made_in_germany(self) -> str:
return "This is a running shoes made by Adidas in Germany."
class AdidasSkateboardingShoes(AdidasShoes):
def made_in_germany(self) -> str:
return "This is a skateboarding shoes made by Adidas in Germany."
class NikeShoes(ABC):
@abstractmethod
def supported_by_ronaldinho(self) -> None:
pass
@abstractmethod
def another_supported_by_ronaldinho(self, collaborator: AdidasShoes) -> None:
pass
"""
Concrete Products are created by corresponding Concrete Factories.
"""
class NikeRunningShoes(NikeShoes):
def supported_by_ronaldinho(self) -> str:
return "This is a Nike Running Shoes and Ronaldinhog like this one."
def another_supported_by_ronaldinho(self, collaborator: AdidasShoes) -> str:
result = collaborator.made_in_germany()
return f"The result of the Nike Running shoes collaborating with the \
({result})"
class NikeSkateboardingShoes(NikeShoes):
def supported_by_ronaldinho(self) -> str:
return "This is a Nike Skateboarding Shoes and Ronaldinhog like this one"
def another_supported_by_ronaldinho(self, collaborator: AdidasShoes):
result = collaborator.made_in_germany()
return f"The result of the Nike Skateboarding shoes collaborating with\
the ({result})"
def client_code(factory: Shoes) -> None:
product_a = factory.create_adidas_shoes()
product_b = factory.create_nike_shoes()
print(f"{product_b.supported_by_ronaldinho()}")
print(f"{product_b.another_supported_by_ronaldinho(product_a)}", end="")
if __name__ == "__main__":
print("Client: Testing client code with the first factory type:")
client_code(RunningFactory())
print("\n")
print("Client: Testing the same client code with the second factory type:")
client_code(SkateboardingFactory())