设计模式-原型模式-prototype-python
来源:互联网 发布:网络兼职赚钱 编辑:程序博客网 时间:2024/06/05 19:34
def
用原型实例指定创建对象的种类, 并且通过拷贝这些原型创建新的对象。
usage
● 性能优良
原型模式是在内存二进制流的拷贝, 要比直接new一个对象性能好很多, 特别是要在一个循环体内产生大量的对象时, 原型模式可以更好地体现其优点。
● 逃避构造函数的约束
这既是它的优点也是缺点, 直接在内存中拷贝, 构造函数是不会执行的 。 优点就是减少了约束, 缺点也是减少了约束, 需要大家在实际应用时考虑。
● 资源优化场景
类初始化需要消化非常多的资源, 这个资源包括数据、 硬件资源等。
● 性能和安全要求的场景
通过new产生一个对象需要非常繁琐的数据准备或访问权限, 则可以使用原型模式。
● 一个对象多个修改者的场景
一个对象需要提供给其他对象访问, 而且各个调用者可能都需要修改其值时, 可以考虑使用原型模式拷贝多个对象供调用者使用。
code
class Prototype(object): value = 'default' def clone(self, **attrs): """Clone a prototype and update inner attributes dictionary""" # Python in Practice, Mark Summerfield obj = self.__class__() obj.__dict__.update(attrs) return objclass PrototypeDispatcher(object): def __init__(self): self._objects = {} def get_objects(self): """Get all objects""" return self._objects def register_object(self, name, obj): """Register an object""" self._objects[name] = obj def unregister_object(self, name): """Unregister an object""" del self._objects[name]def main(): dispatcher = PrototypeDispatcher() prototype = Prototype() d = prototype.clone() a = prototype.clone(value='a-value', category='a') b = prototype.clone(value='b-value', is_checked=True) dispatcher.register_object('objecta', a) dispatcher.register_object('objectb', b) dispatcher.register_object('default', d) print([{n: p.value} for n, p in dispatcher.get_objects().items()])if __name__ == '__main__': main()### OUTPUT #### [{'objectb': 'b-value'}, {'default': 'default'}, {'objecta': 'a-value'}]
阅读全文
0 0
- 设计模式-原型模式-prototype-python
- 设计模式 原型Prototype
- prototype(原型设计模式)
- 设计模式--Prototype(原型)
- Prototype(原型)设计模式
- 设计模式-prototype原型
- 设计模式--原型模式(ProtoType)
- [设计模式]Prototype原型模式
- 设计模式--Prototype(原型模式)
- 设计模式 -- 原型模式Prototype
- 设计模式-原型模式(Prototype)
- [设计模式] 原型模式(Prototype)
- 设计模式-原型模式(Prototype)
- 【设计模式】-原型模式-Prototype
- 设计模式 原型模式 Prototype
- 设计模式:原型模式-prototype
- 设计模式---原型模式(Prototype)
- 设计模式 原型设计 Prototype
- Spring Boot 揭秘与实战(二) 数据存储篇
- 【系统学习SpringBoot】springBoot 入门篇
- js中判断客户端有无安装Adobe 并指引用户安装
- Python-22 文件系统:os模块
- 【干货合集】阿里云专家诠释持续集成与云端交付的正确姿势
- 设计模式-原型模式-prototype-python
- Qt加载翻译文件
- 2017-08-03 面试记录(铃声多多)
- java_UML:继承/泛化、实现、依赖、关联、聚合、组合的联系与区别
- Centos6.5下端口映射操作
- leetcode#160. Intersection of Two Linked Lists
- 配置Struts2+Hibernate
- Objective-C Runtime 运行时之二:成员变量与属性
- wireshark源码探索No.4---packet-ftp.c源码分析