Application Example of prototype in design mode in Python Program

Source: Internet
Author: User
This article mainly introduces the application example of the prototype mode in the design mode in the Python program. This article mainly emphasizes the use of the shortest copy and deep copy in object replication. For more information, see Prototype:
The prototype instance specifies the type of the object to be created and creates a new object by copying the prototype.
The prototype mode is essentially a clone object. Therefore, when the initialization operations of objects are complex, it is very practical and can greatly reduce the time consumption and improve the performance, because "you do not need to reinitialize objects, instead, it dynamically obtains the running state of the object ".

Application features:
A large number of modifications are required based on a basic prototype to obtain the new original type.

Structural Features:
Object replication mechanism, that is, shallow replication and deep replication.

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 = 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__  

Result:

Name1 2 # Because obj2 is a light copy, the object is not copied. As a result, the modification of the new object affects the original object Value name2 2 name3 3 # because it is a deep copy, so it will not affect the old object _ main __. proto_type _ main __. proto_type _ main __. proto_type


Here we will review the knowledge points of the basic Python programming on the shortest copy and deep copy:
Shallow Copy ):
The field of the object is copied, but the object referenced by the field is not copied. The Copied object and the source object only share the same name, but they share the same entity.
Deep copy ):
The object referenced by fields 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 # prototype mode import copy def printInfo (info): print unicode (info, 'utf-8 '). encode ('gbk') # copy Interface class ICloneable: def shallowClone (self): return copy. copy (self) def deepClone (self): return copy. deepcopy (self) # Work Experience class WorkExperience (ICloneable): workData = "" company = "" pass # Resume 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, 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 ('daybird '). setPersonInfo ('male', 29). setWorkExperience ("1998-2000", "XX Company") # copy B =. shallowClone () B. setWorkExperience ("2000-2006", "YY Company") # Deep copy c =. deepClone () c. setWorkExperience ("2006-2009", "ZZ company") B. display (). 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.