-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractFactoryPattern.py
More file actions
126 lines (96 loc) · 3.44 KB
/
Copy pathAbstractFactoryPattern.py
File metadata and controls
126 lines (96 loc) · 3.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Abstract Factory Pattern - provides an interface for creating families of
related or dependent objects without specifying their concrete classes. It
acts like a salesman who creates a generic order form, find clients, take
orders and send the orders to a group of factories to make the products.
Example - Say you have online shops that spread out in different regions.
The shops in different region will sell the same item but different sizing
and cutting to have the best fit for people in the region.
"""
from abc import ABCMeta, abstractmethod
class Website(object):
def placeOrder(self, region):
shop = ""
if region is "europe":
shop = EuropeanShop(EuropeanWarehouse())
elif region is "asia":
shop = AsianShop(AsianWarehouse())
return shop
def getPackage(self, region):
customerPackage = self.placeOrder(region)
customerPackage.fetchItem()
customerPackage.checkItem()
customerPackage.cleanItem()
customerPackage.packageItem()
return customerPackage
class Shop:
__metaclass__ = ABCMeta
@abstractmethod
def fetchItem(self):
pass
def checkItem(self):
print "Checking " + self.sunglass.getType() + " for any defects"
print "Checking " + self.dress.getType() + " for any defects and hide it"
def cleanItem(self):
print "Cleaning up " + self.sunglass.getType() + " using dettol"
print "Cleaning up " + self.dress.getType() + " using bleach"
def packageItem(self):
print "Package " + self.sunglass.getType() + " nicely"
print "Package " + self.dress.getType() + " nicely"
class EuropeanShop(Shop):
def __init__(self, warehouse):
self.warehouse = warehouse
self.region = "Europe"
def fetchItem(self):
print "Getting item from the European Warehouse"
self.sunglass = self.warehouse.getSunglass()
self.dress = self.warehouse.getDress()
class AsianShop(Shop):
def __init__(self, warehouse):
self.warehouse = warehouse
self.region = "Asia"
def fetchItem(self):
print "Getting item from the Asian Warehouse"
self.sunglass = self.warehouse.getSunglass()
self.dress = self.warehouse.getDress()
class Warehouse:
__metaclass__ = ABCMeta
@abstractmethod
def getSunglass(self):
pass
def getDress(self):
pass
class EuropeanWarehouse(Warehouse):
def getSunglass(self):
return EuropeanSunglass()
def getDress(self):
return EuropeanLimitedEditionDress()
class AsianWarehouse(Warehouse):
def getSunglass(self):
return AsianSunglass()
def getDress(self):
return AsianLimitedEditionDress()
class Sunglass:
__metaclass__ = ABCMeta
@abstractmethod
def getType(self):
pass
class EuropeanSunglass(Sunglass):
def getType(self):
return "European Sunglass"
class AsianSunglass(Sunglass):
def getType(self):
return "Asian Sunglass"
class LimitedEditionDress:
__metaclass__ = ABCMeta
@abstractmethod
def getType(self):
pass
class EuropeanLimitedEditionDress(LimitedEditionDress):
def getType(self):
return "European Really Expensive Limited Edition Dress"
class AsianLimitedEditionDress(LimitedEditionDress):
def getType(self):
return "Asian Really Limited Expensive Edition Dress"
website = Website()
website.getPackage("asia")