大话设计模式(Python版)--原型模式

简历代码初步实现--第一版:

#!/usr/bin/env python

class Resume:
    def __init__(self,name):
        self.__name = name
    def SetPersonInfo(self,sex,age):
        self.__sex = sex
        self.__age = age
    def SetWorkExperience(self,timeArea, company):
        self.__timeArea = timeArea
        self.__company = company
    def Display(self):
        print(self.__name,self.__sex,self.__age)
        print("工作经历:",self.__timeArea,self.__company)

def main1():
    resumeA = Resume("大鸟")
    resumeA.SetPersonInfo('男','29')
    resumeA.SetWorkExperience("1998-2000","xx公司")
    resumeB = Resume("大鸟")
    resumeB.SetPersonInfo('男','29')
    resumeB.SetWorkExperience("1998-2000","xx公司")
    resumeC = Resume("大鸟")
    resumeC.SetPersonInfo('男','29')
    resumeC.SetWorkExperience("1998-2000","xx公司")
    resumeA.Display()
    resumeB.Display()
    resumeC.Display()

def main2():
    resumeA = Resume("大鸟")
    resumeA.SetPersonInfo('男','29')
    resumeA.SetWorkExperience("1998-2000","xx公司")    
    resumeB = resumeA
    resumeB.SetWorkExperience('1998-2006','YY公司')    
    resumeC = resumeA
    resumeC.SetPersonInfo('男','24')    
    resumeA.Display()
    resumeB.Display()
    resumeC.Display()
    
if __name__ == '__main__':
    main1()
    main2()
原型模式--第二版

#!/usr/bin/env python
from abc import ABCMeta,abstractmethod
from copy import copy

class Prototype:
    __metaclass__ = ABCMeta
    def __init__(self, Id):
        self.__Id = Id
    @property
    def Id(self):
        return self.__Id
    @abstractmethod
    def Clone(self):
        pass

class ConcretePrototype1(Prototype):
    def __init(self,Id):
        super(ConcretePrototype1,self).__init__(self,Id)
    def Clone(self):
        return copy(self)

if __name__ == '__main__':
    p1 = ConcretePrototype1("I")
    c1 = p1.Clone()
    print("Cloned:",c1.Id)
    
简历的原型模式

#!/usr/bin/env python
from copy import copy

class Resume:
    def __init__(self,name):
        self.__name = name
    def SetPersonalInfo(self, sex, age):
        self.__sex = sex
        self.__age = age
    def SetWorkExperience(self, timeArea, company):
        self.__timeArea = timeArea
        self.__company = company
    def Display(self):
        print(self.__name, self.__sex, self.__age)
        print("工作经历:",self.__timeArea, self.__company)
    def Clone(self):
        return copy(self)

if __name__ == '__main__':
    resumeA = Resume('大鸟')
    resumeA.SetPersonalInfo('男','29')
    resumeA.SetWorkExperience('1998-2000','XX公司')
    resumeB = resumeA.Clone()
    resumeB.SetWorkExperience('1998-2006','YY公司')
    resumeC = resumeA.Clone()
    resumeC.SetPersonalInfo('男','24')
    resumeC.SetWorkExperience('1998-2005','ZZ公司')    
    resumeA.Display()
    resumeB.Display()    
    resumeC.Display()

浅复制与深复制

#!/usr/bin/env python
from copy import copy,deepcopy
class WorkExperience:
    @property
    def workDate(self):
        return self.__workDate
    @workDate.setter
    def workDate(self,value):
        self.__workDate = value
    @property
    def Company(self):
        return self.__company
    @Company.setter
    def Company(self, value):
        self.__company = value

class Resume:
    def __init__(self,name):
        self.__name = name
        self.__work = WorkExperience()
    def SetPersonalInfo(self,sex, age):
        self.__sex = sex
        self.__age = age
    def SetWorkExperience(self, workDate, company):
        self.__work.workDate = workDate
        self.__work.Company = company
    def Display(self):
        print(self.__name,self.__sex,self.__age)
        print(self.__work.workDate, self.__work.Company)
    def Clone(self):
        return copy(self)
    def DeepClone(self):
        return deepcopy(self)    

def main1():
    resumeA = Resume('大鸟')
    resumeA.SetPersonalInfo('男','29')
    resumeA.SetWorkExperience('1998-2000','XX公司')
    resumeB = resumeA.Clone()
    resumeB.SetWorkExperience('1998-2006','YY公司')
    resumeC = resumeA.Clone()
    resumeC.SetPersonalInfo('男','24')
    resumeC.SetWorkExperience('1998-2003','ZZ公司')
    resumeA.Display()
    resumeB.Display()    
    resumeC.Display()

def main2():
    resumeA = Resume('大鸟')
    resumeA.SetPersonalInfo('男','29')
    resumeA.SetWorkExperience('1998-2000','XX公司')
    resumeB = resumeA.DeepClone()
    resumeB.SetWorkExperience('1998-2006','YY公司')
    resumeC = resumeA.DeepClone()
    resumeC.SetPersonalInfo('男','24')
    resumeC.SetWorkExperience('1998-2003','ZZ公司')
    resumeA.Display()
    resumeB.Display()    
    resumeC.Display()
    
   
    
if __name__ == '__main__':
    main1()
    main2()   
   




你可能感兴趣的:(python,设计模式,设计模式)