1.定义:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式的核心是一个clone方法,通过该方法进行对象的拷贝(注意浅拷贝和深拷贝的区别)。
2.原型模式的使用场景:
- 资源优化场景:类初始化需要消耗非常多的资源,这个资源包括数据、硬件资源等。
- 性能和安全要求的场景:通过new产生一个对象需要非常繁琐的数据准备或访问权限。
- 一个对象多个修改者的场景:一个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑用原型模式拷贝多个对象供调用者使用。
在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂方法提供给调用者。
3.原型模式的通用代码:
package _7PrototypePattern;
/**
* 如果需要深克隆请在clone方法中另外实现
*/
public class PrototypeClass implements Cloneable {
@Override
protected PrototypeClass clone() throws CloneNotSupportedException {
return (PrototypeClass)super.clone();
}
}
4.原型模式的优点:
- 性能优良:原型模式是在内存二进制流的拷贝,要比直接new一个对象性能要好很多,特别是要在一个循环体内产生大量对象时,原型模式可以更好的体现其优点。
- 逃避构造函数的约束:这既是它的优点也是缺点,直接在内存中拷贝,构造函数是不会执行的。优点是减少了约束,缺点也是减少了约束,需要在实际使用时考虑。
5.原型模式的注意事项:
- 构造函数不会被执行:这个很好理解,对象是通过clone方法直接在拷贝了二进制流
- 浅拷贝和深拷贝:在适当的地方使用浅拷贝和深拷贝
- clone和final关键字的冲突:对象的clone和对象内的final关键字是有冲突的,Java不允许clone被final修饰的变量。
分享到:
相关推荐
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
6、原型模式(Prototype Pattern) 用意:通过拷贝创建一个新的对象
10. 原型模式 (Prototype Pattern) 11. 组合模式 (Composite Pattern) 12. 装饰者模式 (Decorator Pattern) 13. 访问者模式 (Visitor Pattern) 14. 迭代器模式 (Iterator Pattern) 15. 享元模式 ...
原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式...
原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. ...
5. 原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) ...
原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. ...
原型模式是一种创建型设计模式,它通过复制一个现有的对象来创建新的对象,而不是通过调用构造函数的方式。这种方式可以在运行时动态地创建和修改对象,而不需要知道具体的创建细节 。 原型模式的基本概念包括以下...
原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式...
NULL 博文链接:https://jacky-dai.iteye.com/blog/2295379
.NET设计模式(6):原型模式(PrototypePattern)
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当...
C# 23种设计模式,包括1.单例模式 2.工厂模式3.抽象工厂模式(Abstract Factory Pattern)4.模板方法模式(Template Method Pattern)5....原型模式(Prototype Pattern)8.中介者模式 9.命令模式 等设计模式
原型模式(Prototype Pattern) 2. 结构型模式 适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式(Filter、Criteria Pattern) 组合模式(Composite Pattern) 装饰器模式(Decorator ...
3.设计模式Design Pattern:创建型模式(厂模式Factory、抽象工厂模式Abstract Factory、单例模式Singleton、建造者模式Builder、原型模式Prototype和对象池模式Object Pool Pattern)、结构型模式(适配器模式、...
7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法...
适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...
第 20 章 原型模式【PROTOTYPE PATTERN】 ............................................................................................ 255 第 21 章 中介者模式【MEDIATOR PATTERN】 ...........................
原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 一个原型模式的简单demo: #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'Andy' """ 大话设计模式 ...