``````from enum import Enum

TreeType = Enum('TreeType', 'apple_tree cherry_tree peach_tree')

class Tree(object):
def __init__(self, tree_type, age, x, y):
self.tree_type = tree_type
self.age = age
self.x = x
self.y = y

def render(self):
print('类型 {} 年龄 {} 位置 ({}, {})'.format(self.tree_type, self.age, self.x, self.y))
``````

``````class Tree(object):
pool = dict()

def render(self, age, x, y):
print('类型 {} 年龄 {} 位置 ({}, {})'.format(self.tree_type, age, x, y))

def __new__(cls, tree_type):
t = cls.pool.get(tree_type, None)
if t:
pass
else:
t = object.__new__(cls)
cls.pool[tree_type] = t
t.tree_type = tree_type
return t
``````

`Tree`类如此修改一番，我们就实现了享元模式。简单做一个讲解。

``````def main():
import random
rnd = random.Random()
age_min, age_max = 1, 30    # 单位为年
min_point, max_point = 0, 100
tree_counter = 0

for _ in range(10):
t1 = Tree(TreeType.apple_tree)
t1.render(rnd.randint(age_min, age_max),
rnd.randint(min_point, max_point),
rnd.randint(min_point, max_point))
tree_counter += 1

for _ in range(3):
t2 = Tree(TreeType.cherry_tree)
t2.render(rnd.randint(age_min, age_max),
rnd.randint(min_point, max_point),
rnd.randint(min_point, max_point))
tree_counter += 1

for _ in range(5):
t3 = Tree(TreeType.peach_tree)
t3.render(rnd.randint(age_min, age_max),
rnd.randint(min_point, max_point),
rnd.randint(min_point, max_point))
tree_counter += 1

print('生成并渲染的树木: {}棵'.format(tree_counter))
print('创建的树木对象: {}个'.format(len(Tree.pool)))

t4 = Tree(TreeType.cherry_tree)
t5 = Tree(TreeType.cherry_tree)
t6 = Tree(TreeType.apple_tree)
print('{} == {}? {}'.format(id(t4), id(t5), id(t4) == id(t5)))
print('{} == {}? {}'.format(id(t5), id(t6), id(t5) == id(t6)))
t4.render(4, 4, 4)
t5.render(5, 5, 5)
t6.render(6, 6, 6)
``````

``````\$ python flyweight.py

522952945848 == 522952945848? True
522952945848 == 522954153824? False

``````

``````from enum import Enum

TreeType = Enum('TreeType', 'apple_tree cherry_tree peach_tree')

class Tree(object):
tree_type = ''
pool = dict()

def __init__(self, tree_type, age):
self.age = age

def render(self, x, y):
print('类型 {} 年龄 {} 位置 ({}, {})'.format(self.tree_type, self.age, x, y))

def __new__(cls, tree_type, age):
t = cls.pool.get(tree_type, None)
if t:
pass
else:
t = object.__new__(cls)
cls.pool[tree_type] = t
t.tree_type = tree_type
return t

def main():
import random
rnd = random.Random()
age_min, age_max = 1, 30    # 单位为年
min_point, max_point = 0, 100
tree_counter = 0

for _ in range(10):
t1 = Tree(TreeType.apple_tree,
rnd.randint(age_min, age_max))
t1.render(rnd.randint(min_point, max_point),
rnd.randint(min_point, max_point))
tree_counter += 1

for _ in range(3):
t2 = Tree(TreeType.cherry_tree,
rnd.randint(age_min, age_max))
t2.render(rnd.randint(min_point, max_point),
rnd.randint(min_point, max_point))
tree_counter += 1

for _ in range(5):
t3 = Tree(TreeType.peach_tree,
rnd.randint(age_min, age_max))
t3.render(rnd.randint(min_point, max_point),
rnd.randint(min_point, max_point))
tree_counter += 1

print('生成并渲染的树木: {}棵'.format(tree_counter))
print('创建的树木对象: {}个'.format(len(Tree.pool)))

t4 = Tree(TreeType.cherry_tree,
4)
t5 = Tree(TreeType.cherry_tree,
5)
t6 = Tree(TreeType.apple_tree,
6)
print('{} == {}? {}'.format(id(t4), id(t5), id(t4) == id(t5)))
print('{} == {}? {}'.format(id(t5), id(t6), id(t5) == id(t6)))
t4.render(4, 4)
t5.render(5, 5)
t6.render(6, 6)

if __name__ == '__main__':
main()
``````

``````\$ python flyweight.py

332890664464 == 332890664464? True
332890664464 == 332889074432? False

``````