浅拷贝(Shallow Copy):指对象的字段被拷贝,而字段引用的对象不会被拷贝,拷贝的对象和源对象只是名称相同,但是他们共用一个实体。 浅拷贝会拷贝对象内容及其内容的引用或者子对象的引用,但不会拷贝引用的内容和子对象本身。
深拷贝(deep copy):对对象实例中字段引用的对象也进行拷贝。深拷贝不仅拷贝了对象和内容的引用,也会拷贝引用的内容。所以,一般深拷贝比浅拷贝复制得更加完全,但也更占资源(包括时间和空间资源)。
当我们有一个迭代项目要更新时,第二个版本中的对象实例要在第一个版本的基础上做一些修改时,这个时候我们就可以使用原型模式来实现,从而大大节省了时间,提高了效率。
原型模式用于创建复杂的或者耗时的实例:复制一个已经存在的实例使程序运行更高效。且对于工厂模式,原型模式减少了子类的构建。
每一个产品类都必须配置一个克隆方法,并且这个克隆方法需要对类的功能进行整体考虑。
import copy
from collections import OrderedDict
#定义一个人的类
class Person():
def __init__(self,name,age,high,sex,**info):
self.name=name
self.age=age
self.high=high
self.sex=sex
#可以为实例添加其他额外属性,但必须是key:value格式的属性
self.__dict__.update(info)
#当使用print输出对象的时候,只要自己定义了__str__(self)方法,那么就会打印从在这个方法中return的数据
def __str__(self):
myinfo=[]
#OrderedDict 是 collections 提供的一种数据结构, 它提供了有序的dict结构。
order_dict=OrderedDict(self.__dict__.items())
for i in order_dict.keys():
myinfo.append('{}:{}'.format(i,order_dict[i]))
myinfo.append('\n')
return ''.join(myinfo)
class Prototype():
def __init__(self):
self.objects=dict() #定义原型对象字典表
#在原型字典表中注册原型对象
def register(self,id,obj):
self.objects[id]=obj
#在原型字典表中删除原型对象
def destory(self,id):
del self.objects[id]
#根据 id 在原型字典表中查找原型对象并克隆
def clone(self,id,**atter):
obj1=self.objects.get(id)
print(obj1)
if not obj1:
raise ValueError('Incorrect object id: {}'.format(id))
obj2=copy.deepcopy(obj1)
obj2.__dict__.update(atter)
return obj2
if __name__ == '__main__':
tony=Person('tony',34,170,'man',job='tester',birthday='2000-01-01',hobby='catch fish')
print(tony)
prototype=Prototype()
prototype.register(1001,tony)
tom=prototype.clone(1001,name='tom',age='19',birthday='2010-02-02')
for i in (tony,tom):
print(i)
#输出两个Person对象是否是相同的id值
print("id tony : {} != id tom : {}".format(id(tony), id(tom)))
name:tony
age:34
high:170
sex:man
job:tester
birthday:2000-01-01
hobby:catch fish
name:tony
age:34
high:170
sex:man
job:tester
birthday:2000-01-01
hobby:catch fish
name:tony
age:34
high:170
sex:man
job:tester
birthday:2000-01-01
hobby:catch fish
name:tom
age:19
high:170
sex:man
job:tester
birthday:2010-02-02
hobby:catch fish
id tony : 2930225522448 != id tom : 2930225522504
end