-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple_factory.py
76 lines (63 loc) · 1.59 KB
/
simple_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
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
brief:
Simple factory. Receive a parameter to select
the object you want to constructe.
pattern:
Factory method.
Define an interface for creating an object,
but let subclasses decide which class to
instantiate.Factory Method lets a class defer
instantiation to subclasses
AbstractProduct < = > Factory(for all of the products)
| |
ProductA ProductB
"""
# AbstractProduct
class Animal:
def __init__(self):
self.boundary = 'Animal'
self.state = self.boundary
def __str__(self):
return self.state
# Product
class Lion(Animal):
def __init__(self):
super(Lion, self).__init__();
self.state = self.boundary + ' - Lion'
def __str__(self):
return self.state
class Tiger(Animal):
def __init__(self):
super(Tiger, self).__init__();
self.state = self.boundary + ' - Tiger'
def __str__(self):
return self.state
# Factory
flag = 0
if flag == 0:
class Factory:
def __init__(self):
print('Initialize factory A.')
def create_animal(self, species):
if species == "Lion":
return Lion()
if species == "Tiger":
return Tiger()
else:
class Factory:
def __init__(self):
print('Initialize factory B.')
self.map = {
'Lion': Lion(),
'Tiger': Tiger()
}
def create_animal(self, species):
return self.map[species]
if __name__ == '__main__':
factory = Factory()
obj1 = factory.create_animal("Lion")
obj2 = factory.create_animal("Tiger")
print('obj1: {0}'.format(obj1))
print('obj2: {0}'.format(obj2))