源码地址: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-原型模式的更多相关文章

  1. Python原型模式

    如果想根据现有对象复制出新的对象并对其修改,可以考虑原型模式(Prototype Pattern) class Point: __slots__ = ("x", "y&q ...

  2. python设计模式第六天【原型模式】

    1.定义 使用原型模式复制的对象与原来对象具有一样的结构和数据,有浅克隆和深克隆 2.应用场景 (1)希望复制原来对象的结构和数据胆步影响原来对象 3.代码实现 #!/usr/bin/env pyth ...

  3. [Python设计模式] 第9章 如何准备多份简历——原型模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目 设计一个简历类,必须有姓名,可以设置性别和年龄,即个人信息,可以设置曾就职 ...

  4. 大话设计模式Python实现-原型模式

    原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 一个原型模式的简单demo: #!/usr/bin/env python # -*- c ...

  5. 浅谈Python设计模式 - 原型模式

    声明,本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 在<精通Python设计模式>中把设计模式分为三种类型: 创建型模式 ...

  6. python设计模式之原型模式

    python设计模式之原型模式 ​ 对于原型模式而言,其中最主要的部分就是关于一个对象的复制,其中就包含两个方面:1.浅复制:2.深复制.具体的区别请看我相关的随笔.这里简略的说明一下,浅复制就等于对 ...

  7. python 设计模式之原型模式 Prototype Pattern

    #引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...

  8. 原型模式(python)

    原型模式也叫克隆模式,通过拷贝自身的属性来创建一个新的对象,基本方法就是调用copy模块下的 (浅拷贝)copy() 和(深拷贝)deepcopy() #!/usr/bin/env python3 # ...

  9. 设计模式---对象创建模式之原型模式(prototype)

    一:概念 原型模式(Prototype Pattern) 实际上就是动态抽取当前对象运行时的状态 Prototype模式是一种对象创建型模式,它采取复制原型对象的方法来创建对象的实例.使用Protot ...

  10. 设计模式(六)原型模式(Prototype Pattern)

    一.引言 在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,如果我们用new操作符去创建这样的类实例,这未免会增加创建类的复杂度和耗费更多的内存空间,因为这样在 ...

随机推荐

  1. Mac使用brew安装nginx,并解决端口访问权限问题

    1.安装 brew install nginx 2.修改配置文件 sudo vi /usr/local/etc/nginx/nginx.conf 修改默认的8080端口为80 修改日志文件地方 err ...

  2. Swift5 语言指南(二十一) 嵌套类型

    通常创建枚举以支持特定类或结构的功能.类似地,定义纯粹在更复杂类型的上下文中使用的实用程序类和结构可能是方便的.为此,Swift允许您定义嵌套类型,从而在它们支持的类型定义中嵌套支持枚举,类和结构. ...

  3. 基础I/O接口与操作

    C语言中的文件接口 1 打开文件 FILE * fopen(const char * path,const char * mode) 参数:path:文件路径 mode:打开文件的方式 返回值:成功返 ...

  4. 以太坊ERC20代币合约案例

    一.ERC20代币合约与web3调用 ERC20代币合约在小白看来觉得很高大上,但其实就是一个代币的定义标准,方便其他dapp统一调用各种代币的方法.如图: 二.ERC20合约标准 [官方链接] co ...

  5. 8 Ways to Become a Better Coder

    It’s time to get serious about improving your programming skills. Let’s do it! That’s an easy career ...

  6. K-means算法的原理、优缺点及改进(转)

    文章内容转载自:http://blog.csdn.net/sinat_35512245/article/details/55051306                                ...

  7. vue使用代理实现开发阶段跨域

    在config/index.js找到 proxyTable对象,添加键值对即可. "/api":{ target:"http://192.168.1.1", c ...

  8. vue父组件传参给子组件

    其实组件之间传参有很多种方法: 1.通过本地存储 2.使用vuex状态管理 今天记录一下第三种方法 1.首页我们先创建一个项目(创建项目自行百度) 2.打开项目,在components文件夹下新建一个 ...

  9. 音频播放封装(pcm格式,Windows平台 c++)

    介绍 pcm格式是音频非压缩格式.如果要对音频文件播放,需要先转换为pcm格式. windows提供了多套函数用于播放,本文介绍Waveform Audio Functions系列函数. 原始的播放函 ...

  10. Collections.sort()中的mergeSort归并排序

    @SuppressWarnings({"unchecked", "rawtypes"}) private static void mergeSort(Objec ...