时间:2021-07-01 10:21:17 帮助过:63人阅读
应用特性:
需要大量的基于某个基础原型进行微量修改而得到新原型时使用。
结构特性:
对象的复制机制,即浅复制和深复制。
例1:
#!/usr/bin/env python #encoding: utf-8 from copy import copy, deepcopy class test_obj: def __init__(self, id): self.id = id class proto_type: def __init__(self, name, id): self.name = name self.obj = test_obj(id) def display(self): print self.name print self.obj.id def clone(self): return copy(self) def deep_clone(self): return deepcopy(self) if '__main__' == __name__: obj1 = proto_type('name1', 1) obj2 = obj1.clone() obj3 = obj1.deep_clone() obj2.name = 'name2' obj2.obj.id = 2 obj3.name = 'name3' obj3.obj.id = 3 obj1.display() obj2.display() obj3.display() print obj1.__class__ print obj2.__class__ print obj3.__class__
结果:
name1 2 #因为obj2是浅复制,所以对象没有被复制,导致新对象的修改影响了原来的就对象的值 name2 2 name3 3 #因为是深复制,所以不会影响之前的旧对象 __main__.proto_type __main__.proto_type __main__.proto_type
这里我们再来回顾一下Python编程基础中关于浅拷贝和深拷贝的知识点:
浅拷贝(Shallow Copy):
指对象的字段被拷贝,而字段引用的对象不会被拷贝,拷贝的对象和源对象只是名称相同,但是他们共用一个实体。
深拷贝(deep copy):
对对象实例中字段引用的对象也进行拷贝。
好了,基于以上,我们再来看一个例子:
例2:
#encoding=utf-8 # #by panda #原型模式 import copy def printInfo(info): print unicode(info, 'utf-8').encode('gbk') #拷贝接口 class ICloneable: def shallowClone(self): return copy.copy(self) def deepClone(self): return copy.deepcopy(self) #工作经历 class WorkExperience(ICloneable): workData = "" company = "" pass #简历 class Resume(ICloneable): name = "" sex = '未知' age = 0 work = None def __init__(self, name, work = WorkExperience()): self.name = name self.work = work; def setPersonInfo(self, sex, age): self.sex = sex self.age = age def setWorkExperience(self, workData, company): self.work.workData = workData self.work.company = company def display(self): printInfo('%s, %s, %d' % (self.name,self.sex,self.age)) printInfo('%s, %s' % (self.work.workData, self.work.company)) def clientUI(): a = Resume('大鸟') a.setPersonInfo('男',29) a.setWorkExperience("1998-2000","XX公司") #浅拷贝 b = a.shallowClone() b.setWorkExperience("2000-2006","YY公司") #深拷贝 c = a.deepClone() c.setWorkExperience("2006-2009","ZZ公司") b.display() a.display() c.display() return if __name__ == '__main__': clientUI();