Example of application of prototype patterns in design patterns in Python programs

Source: Internet
Author: User
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 ();
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.