forked from gennad/Design-Patterns-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flyweight.py
59 lines (47 loc) · 1.71 KB
/
flyweight.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
"""
Flyweight Design Pattern
Desc: Sharing the shareable data between the common classes and thus
reducing the memory usage
Code: Believing that every person in a family will have same genetic
structure, we will create a code to learn about
genetics of each family. If a same member of a family is given, no new
object is created. (You can also create new
objects unlike here but keep a reference of the heavy weighted one in
the new |||r object).
"""
class ComplexGenetics(object):
"""Returns a huge genetic pattern"""
def __init__(self):
pass
def genes(self, gene_code):
return "ComplexPatter[%s]TooHugeinSize" % (gene_code)
class Families(object):
"""To learn about Family Genetic Pattern."""
family = {}
def __new__(cls, name, family_id):
"""I have to capture the details before the class is created, __init__
is pseudo constructor."""
try:
id = cls.family[family_id]
except KeyError:
id = object.__new__(cls)
cls.family[family_id] = id
return id
def set_genetic_info(self, genetic_info):
cg = ComplexGenetics()
self.genetic_info = cg.genes(genetic_info)
def get_genetic_info(self):
return (self.genetic_info)
def test():
data = (('a', 1, 'ATAG'), ('a', 2, 'AAGT'), ('b', 1, 'ATAG'))
family_objects = []
for i in data:
obj = Families(i[0], i[1])
obj.set_genetic_info(i[2])
family_objects.append(obj)
for i in family_objects:
print "id = " + str(id(i))
print i.get_genetic_info()
print "similar id's says that they are same objects "
if __name__ == '__main__':
test()