-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_factory.py
145 lines (106 loc) · 3 KB
/
abstract_factory.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
Car
Benz
Suv => gla | glc
Coupe => cls | E-class
BMW
Suv => x1 | x2
Coupe => m2 | m4
"""
from abc import ABC, abstractmethod
# Car Company
class Car(ABC):
@abstractmethod
def call_suv(self):
pass
@abstractmethod
def call_coupe(self):
pass
class Benz(Car):
@staticmethod
def call_suv(model):
return model
@staticmethod
def call_coupe(model):
return model
class BMW(Car):
@staticmethod
def call_suv(model):
return model
@staticmethod
def call_coupe(model):
return model
# Car Classification
class Suv(ABC):
@abstractmethod
def creating_suv(self):
pass
class Coupe(ABC):
@abstractmethod
def creating_coupe(self):
pass
# Car Models
class Gla(Suv, Benz):
def creating_suv(self):
print('This is your suv benz, gla model...')
class Glc(Suv, Benz):
def creating_suv(self):
print('This is your suv benz, glc model...')
class X1(Suv, BMW):
def creating_suv(self):
print('This is your suv bmw, x1 model...')
class X2(Suv, BMW):
def creating_suv(self):
print('This is your suv bmw, x2 model...')
class Cls(Coupe, Benz):
def creating_coupe(self):
return print('This is your coupe benz, cls model....')
class EClass(Coupe, Benz):
def creating_coupe(self):
return print('This is your coupe benz, E-class model....')
class M2(Coupe, BMW):
def creating_coupe(self):
return print('This is your coupe bmw, m2 model....')
class M4(Coupe, BMW):
def creating_coupe(self):
return print('This is your coupe bmw, m4 model....')
# Orders
def order_suv(company: Car, model: Suv):
model_name = model().__class__.__name__
company_name = company().__class__.__name__
if issubclass(model, company):
company: Car = company()
model: Suv = model()
suv: Suv = company.call_suv(model)
try:
suv.creating_suv()
except AttributeError:
raise ValueError(f"{model_name} is not a suv car!")
else:
raise NameError(f"{model_name} is not a {company_name} car!")
def order_coupe(company: Car, model: Coupe):
model_name = model().__class__.__name__
company_name = company().__class__.__name__
if issubclass(model, company):
company: Car = company()
model: Coupe = model()
coupe: Coupe = company.call_coupe(model)
try:
coupe.creating_coupe()
except AttributeError:
raise ValueError(f"{model_name} is not a coupe car!")
else:
raise NameError(f"{model_name} is not a {company_name} car!")
if __name__ == '__main__':
order_coupe(Benz, Cls)
order_coupe(Benz, EClass)
order_coupe(BMW, M2)
order_coupe(BMW, M4)
order_suv(BMW, X1)
order_suv(BMW, X2)
order_suv(Benz, Gla)
order_suv(Benz, Glc)
# order_coupe(Benz, Gla)
# order_coupe(BMW, Cls)
# order_suv(Benz, Cls)
# order_suv(BMW, Gla)