-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathabstract_factory_pattern.py
93 lines (58 loc) · 1.81 KB
/
abstract_factory_pattern.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
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
"""
Provide an interface for creating families of related or dependent
objects without specifying their concrete classes.
"""
from abc import ABCMeta, abstractmethod
class CarFactory(metaclass=ABCMeta):
@abstractmethod
def build_parts(self):
pass
@abstractmethod
def build_car(self):
pass
class SedanCarFactory(CarFactory):
def build_parts(self):
return SedanCarPartsFactory()
def build_car(self):
return SedanCarAssembleFactory()
class SUVCarFactory(CarFactory):
def build_parts(self):
return SUVCarPartsFactory()
def build_car(self):
return SUVCarAssembleFactory()
class CarPartsFactory(metaclass=ABCMeta):
"""
Declare an interface for a type of car parts.
"""
@abstractmethod
def build(self):
pass
class SedanCarPartsFactory(CarPartsFactory):
def build(self):
print("Sedan car parts are built")
def __str__(self):
return '<Sedan car parts>'
class SUVCarPartsFactory(CarPartsFactory):
def build(self):
print("SUV Car parts are built")
def __str__(self):
return '<SUV car parts>'
class CarAssembleFactory(metaclass=ABCMeta):
"""
Declare an interface for a type of cars.
"""
@abstractmethod
def assemble(self):
pass
class SedanCarAssembleFactory(CarAssembleFactory):
def assemble(self, parts):
print(f"Sedan car is assembled here using {parts}")
class SUVCarAssembleFactory(CarAssembleFactory):
def assemble(self, parts):
print(f"SUV car is assembled here using {parts}")
if __name__ == "__main__":
for factory in (SedanCarFactory(), SUVCarFactory()):
car_parts = factory.build_parts()
car_parts.build()
car_builder = factory.build_car()
car_builder.assemble(car_parts)