-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfactory_method.py
70 lines (58 loc) · 1.58 KB
/
factory_method.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
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
brief:
Factory method. Use multiple factories for
multiple products
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 < = > AbstractFactory
| | | |
ProductA ProductB FactoryA(for ProductA) FactoryB(for 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
# AbstractFactory
class Factory():
def create_animal(self):
pass
# Factory
class LionFactory(Factory):
def __init__(self):
print('Initialize LionFactory.')
def create_animal(self):
return Lion()
class TigerFactory(Factory):
def __init__(self):
print('Initialize TigerFactory.')
def create_animal(self):
return Tiger()
if __name__ == '__main__':
lion_factory = LionFactory()
tiger_factory = TigerFactory()
obj1 = lion_factory.create_animal()
obj2 = tiger_factory.create_animal()
print('obj1: {0}'.format(obj1))
print('obj2: {0}'.format(obj2))