Python 原型模式

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 28 19:41:57 2018

@author: mz
"""

from copy import copy, deepcopy

class Prototype(object):
    def Clone(self):
        pass
    
    def DeepClone(self):
        pass
    
class Year(object):
    
    @property
    def year(self):
        return self._year

    @year.setter
    def year(self, y):
        if isinstance(y, int):
            self._year = y
            return
        if isinstance(y, str) and y.isdigit():
            y = int(y)
            self._year = y
        else:
            raise ValueError("year is illegal")

    @year.deleter
    def year(self):
        del self._year    
    

class Resume(Prototype):
    
    def __init__(self, name):
        self.name = name   
        self.workingYear = 3
        self._year = Year()
        self._year.year = 3
        
        
    def SetWorkingYear(self, y):
        self.workingYear = y
        self._year.year = y
    
    def Clone(self):
        return copy(self)
    
    def DeepClone(self):
        return deepcopy(self)
    
    def Show(self):
        print(self.name, self.workingYear, self._year.year)

if "__main__" == __name__:
    resume = Resume("zhang san")    
    resume.Show()
    
    a = resume.Clone()
    b = resume.DeepClone()
    
    print("\r\n---resume 10---")
    resume.SetWorkingYear(10)     
    resume.Show()
    a.Show()
    b.Show()
    
    print("\r\n---clone a 8---")
    a.SetWorkingYear(8)
    resume.Show()
    a.Show()
    b.Show()
    
    print("\r\n---deep clone b 6---")
    b.SetWorkingYear(6)
    resume.Show()
    a.Show()
    b.Show()  

运行结果:

zhang san 3 3

---resume 10---
zhang san 10 10
zhang san 3 10
zhang san 3 3

---clone a 8---
zhang san 10 8
zhang san 8 8
zhang san 3 3

---deep clone b 6---
zhang san 10 8
zhang san 8 8
zhang san 6 6
原型模式是在模板的基础上,对自身进行重新拷贝。
1 对于值的拷贝,没有影响。
2 但是对于对象,分为浅拷贝和深拷贝。浅拷贝和深拷贝的区别就是看是否重新申请了一份内存空间,拷贝的对象是否和原型独立。深拷贝是独立的一个内存空间,其所有值和原型一样。浅拷贝则和原型共用一个内存空间。

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