forked from amirbigg/python-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract factory.py
More file actions
90 lines (63 loc) · 1.52 KB
/
abstract factory.py
File metadata and controls
90 lines (63 loc) · 1.52 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
"""
Abstract Factory
- Abstract Factory Pattern serves to provide an interface for creating related/dependent
objects without need to specify their actual class.
Car => Benz, Bmw => Suv, Coupe
benz suv => gla
bmw suv => x1
benz coupe => cls
bmw coupe => m2
"""
from abc import ABC, abstractmethod
class Car(ABC): # Abstract Factory
@abstractmethod
def call_suv(self):
pass
@abstractmethod
def call_coupe(self):
pass
class Benz(Car): # Concrete Factory1
def call_suv(self):
return Gla()
def call_coupe(self):
return Cls()
class Bmw(Car): # Concrete Factory2
def call_suv(self):
return X1()
def call_coupe(self):
return M2()
class Suv(ABC): # Abstract Product A
@abstractmethod
def create_suv(self):
pass
class Coupe(ABC): # Abstract Product B
@abstractmethod
def create_coupe(self):
pass
class Gla(Suv): # Concrete Product A1
def create_suv(self):
print('This is your suv benz gla...')
class X1(Suv): # Concrete Product A2
def create_suv(self):
print('This is your suv bmw x1...')
class Cls(Coupe): # Concrete Product B1
def create_coupe(self):
print('This is your coupe benz cls...')
class M2(Coupe): # Concrete Product B2
def create_coupe(self):
print('This is your coupe bmw m2...')
def client_suv(order): # Client
brands = {
'benz': Benz,
'bmw': Bmw
}
suv = brands[order]().call_suv()
suv.create_suv()
def client_coupe(order): # Client
brands = {
'benz': Benz,
'bmw': Bmw
}
coupe = brands[order]().call_coupe()
coupe.create_coupe()
client_coupe('benz')