-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.py
More file actions
193 lines (129 loc) · 3.74 KB
/
Copy pathfactory.py
File metadata and controls
193 lines (129 loc) · 3.74 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# code: utf-8
"""
factory model study
"""
from abc import ABCMeta, abstractmethod
# simple factory
class Animal(object):
__metaclass__ = ABCMeta
@abstractmethod
def do_say(self):
pass
class Dog(Animal):
def do_say(self):
print "Bhow Bhow!!"
class Cat(Animal):
def do_say(self):
print "Meow Meow!!"
# forest factory defined
class ForesFactory(object):
"""Factory"""
def make_sound(self, object_type):
return eval(object_type)().do_say()
# return Animal.register(object_type)
# factory
class Section(object):
__metaclass__ = ABCMeta
@abstractmethod
def describe(self):
pass
class PersonalSection(Section):
def describe(self):
print "Personal Section"
class AlbumSection(Section):
def describe(self):
print "Album Section"
class PatenSection(Section):
def describe(self):
print "Paten Section"
class PublicationSection(Section):
def describe(self):
print "Publication Section"
class Profile(object):
__metaclass__ = ABCMeta
def __init__(self):
self.sections = []
self.createProfile()
@abstractmethod
def createProfile(self):
pass
def getSections(self):
return self.sections
def addSections(self, section):
self.sections.append(section)
class linkedin(Profile):
def createProfile(self):
self.addSections(PersonalSection)
self.addSections(PatenSection)
self.addSections(PublicationSection)
class facebook(Profile):
def createProfile(self):
self.addSections(PersonalSection)
self.addSections(AlbumSection)
def SignIn(profile):
return eval(profile.lower())()
# abstract factory
class PizzaFactory(object):
__metaclass__ = ABCMeta
@abstractmethod
def createVegPizza(self):
pass
# @abstractmethod
def createNonVegPizza(self):
pass
class IndianPizzaFactory(PizzaFactory):
def createVegPizza(self):
return DeluxVeggiePizza()
def createNonVegPizza(self):
return ChickenPizza()
class USPizzaFactory(PizzaFactory):
def createVegPizza(self):
return MexicanVegPizza()
def createNonVegPizza(self):
return HamPizza()
class VegPizza(object):
__metaclass__ = ABCMeta
@abstractmethod
def prepare(self, VegPizza):
pass
class DeluxVeggiePizza(VegPizza):
def prepare(self, VegPizza):
print "prepare: ", type(self).__name__
class MexicanVegPizza(VegPizza):
def prepare(self, VegPizza):
print "prepare: ", type(self).__name__
class NonVegPizza(object):
__metaclass__ = ABCMeta
@abstractmethod
def serve(self, NonVegPizza):
pass
class ChickenPizza(NonVegPizza):
def serve(self, NonVegPizza):
print type(self).__name__, "is served with Chicken on", type(NonVegPizza).__name__
class HamPizza(NonVegPizza):
def serve(self, NonVegPizza):
print type(self).__name__, "is serve with Ham on", type(NonVegPizza).__name__
class PizzaStore(object):
def __init__(self):
pass
def makePizza(self):
for factory in [IndianPizzaFactory(), USPizzaFactory()]:
self.factory = factory
self.NonVegPizza = self.factory.createNonVegPizza()
self.VegPizza = self.factory.createVegPizza()
self.VegPizza.prepare(self.VegPizza)
self.NonVegPizza.serve(self.NonVegPizza)
# client code
if __name__ == "__main__":
# simple factory
ff = ForesFactory()
ff.make_sound("Cat")
ff.make_sound("Dog")
# factory
aa = SignIn('LinkedIn')
print aa.getSections()
bb = SignIn('FaceBook')
print bb.getSections()
# abstract factory
pizza_store = PizzaStore()
pizza_store.makePizza()