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原型模式
如果想根据现有对象复制出新的对象并对其修改,可以考虑原型模式(Prototype Pattern) class Point: __slots__ = ("x", "y&q ...
- python设计模式第六天【原型模式】
1.定义 使用原型模式复制的对象与原来对象具有一样的结构和数据,有浅克隆和深克隆 2.应用场景 (1)希望复制原来对象的结构和数据胆步影响原来对象 3.代码实现 #!/usr/bin/env pyth ...
- [Python设计模式] 第9章 如何准备多份简历——原型模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 设计一个简历类,必须有姓名,可以设置性别和年龄,即个人信息,可以设置曾就职 ...
- 大话设计模式Python实现-原型模式
原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 一个原型模式的简单demo: #!/usr/bin/env python # -*- c ...
- 浅谈Python设计模式 - 原型模式
声明,本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 在<精通Python设计模式>中把设计模式分为三种类型: 创建型模式 ...
- python设计模式之原型模式
python设计模式之原型模式 对于原型模式而言,其中最主要的部分就是关于一个对象的复制,其中就包含两个方面:1.浅复制:2.深复制.具体的区别请看我相关的随笔.这里简略的说明一下,浅复制就等于对 ...
- python 设计模式之原型模式 Prototype Pattern
#引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...
- 原型模式(python)
原型模式也叫克隆模式,通过拷贝自身的属性来创建一个新的对象,基本方法就是调用copy模块下的 (浅拷贝)copy() 和(深拷贝)deepcopy() #!/usr/bin/env python3 # ...
- 设计模式---对象创建模式之原型模式(prototype)
一:概念 原型模式(Prototype Pattern) 实际上就是动态抽取当前对象运行时的状态 Prototype模式是一种对象创建型模式,它采取复制原型对象的方法来创建对象的实例.使用Protot ...
- 设计模式(六)原型模式(Prototype Pattern)
一.引言 在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,如果我们用new操作符去创建这样的类实例,这未免会增加创建类的复杂度和耗费更多的内存空间,因为这样在 ...
随机推荐
- 修改oracle默认监听端口
修改oracle默认监听端口 oracle端口修改 主要是修改两个文件和修改oracle参数local_listener 1 查看当前监听状态 [oracle@test ~]$ lsnrctl sta ...
- java后端导出excel表格
转载 :https://www.cnblogs.com/zhaoyuwei/p/9038135.html 不需要在实体类些@Excel(name = "登录名", width = ...
- webApp开发中的总结
meta标签: H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wid ...
- Liferay-Activiti 企业特性功能介绍 (新版Liferay7)
前言 如果你是开发者 你已经是多少次开发一个项目,一次次的用一些框架,一次次的写类似的重复的代码,一次次建表\写类和方法\写HTML\CSS\JAVASCRIPT,一次次测试,一次次的写Bug...如 ...
- [原创]内网渗透JSP webSehll连接工具
工具: JspShellExec编译: VS2012 C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.b ...
- C++:实现类似MFC的IsKindOf功能
假设需要一个类别库,改类别库共包含以下5个类:GrandFather(祖父类).Father(父类).Son(儿子类).Daughter(女儿类).GrandSon(孙子类) 各个类之间的继承关系为: ...
- arm 算力运算
MIPS: Million Instructions executed Per SecondDMIPS: Dhrystone Million Instructions executed Per Sec ...
- Quarz.net 设置任务并行和任务串行
如何设置Quarz.net某个任务完成后再继续执行该任务? Quarz.net 的任务有并行和串行两种: 并行:一个定时任务,当执行时间到了的时候,立刻执行此任务,不管当前这个任务是否在执行中: 串 ...
- 关于 ASP.NET Web 应用中 async/await 注意问题
System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.T ...
- html中引入调用另一个公用html模板文件的方法
html中引入调用另一个公用html模板文件的方法 https://www.w3h5.com/post/53.html 这里我使用jquery的方法 <body> <div id=& ...