python-原型模式

Posted 蔚蓝的蓝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-原型模式相关的知识,希望对你有一定的参考价值。

源码地址:https://github.com/weilanhanf/PythonDesignPatterns

说明

原型模式关注的是大量相同对象或相似对象的创建问题,意图在于通过复制一个已经存在的实例来获得一个新的实例,以避免重复创建此类实例带来的开销。被复制的实例就是这个“原型”,这个原型是可定制的。

 

实例:

在Photoshop等平面设计的软件中,图层概念的提出,使得设计、图形修改等操作更加便利。设计师既可以修改和绘制当前图像对象,又可以保留其它图像对象,逻辑清晰,且可以及时得到反馈。示例中一图层为主角,介绍原型模式。 

 

from copy import copy, deepcopy

class simpleLayer:
    """
       设计一个图层对象,用background表示背景的RGBA,简单用content表示内容,除了直接绘画,还可以设置透明度。
  """
    background=[0,0,0,0]
    content="blank"
    def getContent(self):
        return self.content
    def getBackground(self):
        return self.background
    def paint(self,painting):
        self.content=painting
    def setParent(self,p):
        self.background[3]=p
    def fillBackground(self,back):
        self.background=back
    def clone(self):
        return copy(self)
    def deep_clone(self):
        return deepcopy(self)


if  __name__=="__main__":
    dog_layer=simpleLayer()
    dog_layer.paint("Dog")
    dog_layer.fillBackground([0,0,255,0])
    print("Background:",dog_layer.getBackground())
    print("Painting:",dog_layer.getContent())
    another_dog_layer=dog_layer.clone()
    # 通过复制(clone)这个动作实现画一只同样的狗
    print("Background:", another_dog_layer.getBackground())
    print("Painting:", another_dog_layer.getContent())

 

运行结果为:

Background: [0, 0, 255, 0]
Painting: Dog
Background: [0, 0, 255, 0]
Painting: Dog

 

 

深浅拷贝:

 

浅拷贝会拷贝对象内容及其内容的引用或者子对象的引用,但不会拷贝引用的内容和子对象本身;而深拷贝不仅拷贝了对象和内容的引用,也会拷贝引用的内容。所以,一般深拷贝比浅拷贝复制得更加完全,但也更占资源(包括时间和空间资源)。

#浅拷贝
if
__name__=="__main__": dog_layer=simpleLayer() dog_layer.paint("Dog") dog_layer.fillBackground([0,0,255,0]) print("Original Background:",dog_layer.getBackground()) print("Original Painting:",dog_layer.getContent()) another_dog_layer=dog_layer.clone() # another_dog_layer=dog_layer.deep_clone() another_dog_layer.setParent(128) another_dog_layer.paint("Puppy") print("Original Background:", dog_layer.getBackground()) print("Original Painting:", dog_layer.getContent()) print("Copy Background:", another_dog_layer.getBackground()) print("Copy Painting:", another_dog_layer.getContent())

运行结果:

Original Background: [0, 0, 255, 0]
Original Painting: Dog
Original Background: [0, 0, 255, 128]
Original Painting: Dog
Copy Background: [0, 0, 255, 128]
Copy Painting: Puppy

可以看出在浅拷贝时setParent()方法在类another_dog_layer中并没有二次执行,而是直接从内存中dog_layer中拷贝过来(用的类dog_layer的)

#深拷贝
if  __name__=="__main__":
    dog_layer=simpleLayer()
    dog_layer.paint("Dog")
    dog_layer.fillBackground([0,0,255,0])
    print("Original Background:",dog_layer.getBackground())
    print("Original Painting:",dog_layer.getContent())
    #another_dog_layer=dog_layer.clone()
    another_dog_layer=dog_layer.deep_clone()
    another_dog_layer.setParent(128)
    another_dog_layer.paint("Puppy")
    print("Original Background:", dog_layer.getBackground())
    print("Original Painting:", dog_layer.getContent())
    print("Copy Background:", another_dog_layer.getBackground())
    print("Copy Painting:", another_dog_layer.getContent())

执行结果:

Original Background: [0, 0, 255, 0]
Original Painting: Dog
Original Background: [0, 0, 255, 0]
Original Painting: Dog
Copy Background: [0, 0, 255, 128]
Copy Painting: Puppy

可以看出,再深拷贝时another_dog_layer的所有方法都是自己构建的。

 

优点:

原型模式用于创建复杂的或者耗时的实例:复制一个已经存在的实例使程序运行更高效。性对于工厂模式,原型模式减少了子类的构建。

使用

1、当一个系统应该独立于他的产品的创建,构成,和表示时;

2、当实例化的类是在运行时刻指定时,例如通过动态装载;

3、为了避免创建一个与产品类层次平行的工厂类层次时;

缺点:

每一个产品类都必须配置一个克隆方法,并且这个克隆方法需要对类的功能进行整体考虑。c

参考链接:https://yq.aliyun.com/articles/70451?spm=a2c4e.11155435.0.0.c3e038dajAofdY

以上是关于python-原型模式的主要内容,如果未能解决你的问题,请参考以下文章

Python 设计模式 — 创建型模式 — 原型模式

Python 设计模式 — 创建型模式 — 原型模式

python设计模式第六天原型模式

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

原型模式

python-原型模式