Prototype mode:
The prototype instance specifies the kind of object created and creates a new object by copying the prototypes.
Prototype mode is essentially cloning objects, so in the case of complex object initialization, it is practical, can greatly reduce time-consuming, improve performance, because "do not have to reinitialize the object, but dynamically get the state of the object runtime."
Application Features:
It takes a lot of minor modifications based on a base prototype to be used when a new prototype is obtained.
Structural characteristics:
The replication mechanism for objects, that is, shallow copy and deep copy.
Example 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 = t Est_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< C22/>obj3.name = ' Name3 ' obj3.obj.id = 3 obj1.display () obj2.display () obj3.display () Print obj1.__class__ print obj2.__class__ print obj3.__class__
Results:
Name1 2 #因为obj2是浅复制, so the object is not copied, causing the modification of the new object to affect the original value of the object Name2 2 Name3 3
Here we come back to the basics of Python programming about shallow and deep copies:
Shallow copy (shallow copy):
The object's field is copied, and the object referenced by the field is not copied, the copied object and the source object are just the same name, but they share an entity.
Deep copy:
The object referenced by the field in the object instance is also copied.
Well, based on the above, let's look at an example:
Example 2:
#encoding =utf-8 # #by panda #原型模式 Import Copy def printinfo (info): Print Unicode (info, ' utf-8 '). Encode (' GBK ') #拷贝接口 C Lass Icloneable:def Shallowclone (self): return copy.copy (self) def deepclone (self): return Copy.deepcopy (S ELF) #工作经历 class Workexperience (icloneable): Workdata = "Company =" "Pass #简历 class Resume (icloneable): Name = "" Sex = ' unknown ' 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, compan Y): 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 (' Big Bird ') a.setpersoninfo (' Man ') a.setworkexperience ("1998-2000", "xx company") #浅拷贝 B = A.shallowclone () b.sEtworkexperience ("2000-2006", "yy Company") #深拷贝 C = A.deepclone () c.setworkexperience ("2006-2009", "zz Company") b. Display () A.display () C.display () return if __name__ = = ' __main__ ': Clientui ();