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操作符去创建这样的类实例,这未免会增加创建类的复杂度和耗费更多的内存空间,因为这样在 ...
随机推荐
- 传输层两大协议:TCP和UDP
1. UDP 1.1 发送方式(如何发送) 面向无连接. 无状态服务. 不保证不丢失,不保证按顺序到达. 1.2 发送形式(发送的是什么) 基于数据报. 一个一个的发送,一个一个的接收. 1.3 使用 ...
- Linux - route & traceroute & ip
route route - show / manipulate the IP routing table route 命令常用命令示例 #显示路由 route route -n # 不解析名字,快速显 ...
- 基于Django+celery二次开发动态配置定时任务 ( 一 )
需求: 前端时间由于开发新上线一大批系统,上完之后没有配套的报表系统.监控,于是乎开发.测试.产品.运营.业务部.财务等等各个部门就跟那饥渴的饿狼一样需要 各种各样的系统数据满足他们.刚开始一天一个还 ...
- odoo-开发笔记 列表视图 增加记录弹出窗口效果
editable="bottom" 增加该标签的效果是,添加记录的时候,在原列表视图上一行一行添加; 去掉该标签之后,那么增加新记录的时候,会以弹出窗口的方式实现. 如果弹出的窗口 ...
- 计算机网络 之 TCP和UDP的端口号解析
前言:今天了解一下tcp和udp报文的端口.发现一直以来都只是知道端口用于区分同一IP的服务器的不同服务,已经端口的大小.在查找traceroute的资料的时候,才了解到一些之前没注意到的东西. (一 ...
- JavaScript概念之screen/client/offset/scroll/inner/avail的width/left 分类: JavaScript HTML+CSS 2015-05-27 16:42 635人阅读 评论(0) 收藏
原文地址:http://caibaojian.com/js-name.html JS中获取各种宽度和距离,常常让我们混淆,各种浏览器的不兼容让我们很头疼,现在就在说说js中有哪些宽度和距离. 1.名词 ...
- Tomcat笔记:Tomcat的执行流程解析
Bootstrap的启动 Bootstrap的main方法先new了一个自己的对象(Bootstrap),然后用该对象主要执行了四个方法: init(); setAwait(true); load(a ...
- MySQL Replication 详解MySQL数据库设置主从同步的方法
MySQL同步的流程大致如下: 1.主服务器(master)将变更事件(更新.删除.表结构改变等等)写入二进制日志(master log). 2.从服务器(slave)的IO线程从主服务器(binl ...
- Linux下解决高并发socket最大连接数限制,tcp默认1024个连接
linux作为服务器系统,当socket运行高并发TCP程序时,通常会出现连接建立到一定个数后不能再建立连接的情况 本人在工作时,测试高并发tcp程序(GPS服务器端程序),多次测试,发现每次连接建立 ...
- Mybatis在非spring环境下配置文件中使用外部数据源(druidDatasource)
Spring环境下, MyBatis可以通过其本身的增强mybatis-spring提供的org.mybatis.spring.SqlSessionFactoryBean来注入第三方DataSourc ...