原型模式ProtoType
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/3/4 21:49
# @Author : ChenAdong
# @email : aiswell@foxmail.com
import copy
class ProtoType:
def __init__(self):
self._object = {}
def register_object(self, name, obj):
self._object[name] = obj
def unregister_object(self, name):
del self._object[name]
def clone(self, name, **attr):
_obj = copy.deepcopy(self._object.get(name))
print(_obj)
_obj.__dict__.update(attr)
return _obj
if __name__ == "__main__":
class Test:
pass
t = Test()
prototype = ProtoType()
prototype.register_object("c", t)
t1 = prototype.clone("c", attr_1=1)
print(t1.attr_1)
<__main__.Test object at 0x000002340CDF2630>
1

浙公网安备 33010602011771号