创建型模式:Prototype(原型)
来源:互联网 发布:钱学森 气功 知乎 编辑:程序博客网 时间:2024/05/17 23:20
WIKI:http://zh.wikipedia.org/wiki/%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F
原型模式是创建型模式的一种,其特点在于通过「复制」一个已经存在的实例来返回新的实例,而不是新建实例。被复制的实例就是我们所称的「原型」,这个原型是可定制的。
原型模式多用于创建复杂的或者耗时的实例,因为这种情况下,复制一个已经存在的实例使程序运行更高效;或者创建值相等,只是命名不一样的同类数据。
/** Prototype Class **/public class Cookie implements Cloneable { public Object clone() { try{ //In an actual implementation of this pattern you would now attach references to //the expensive to produce parts from the copies that are held inside the prototype. return this.getClass().newInstance(); } catch(InstantiationException e) { e.printStackTrace(); return null; } }}/** Concrete Prototypes to clone **/public class CoconutCookie extends Cookie { }/** Client Class**/public class CookieMachine{ private Cookie cookie;//cookie必须是可复制的 public CookieMachine(Cookie cookie) { this.cookie = cookie; } public Cookie makeCookie() { return (Cookie)cookie.clone(); } public Object clone() { } public static void main(String args[]){ Cookie tempCookie = null; Cookie prot = new CoconutCookie(); CookieMachine cm = new CookieMachine(prot); //设置原型 for(int i=0; i<100; i++) tempCookie = cm.makeCookie();//通过复制原型返回多个cookie } }
- 创建型模式-----原型模式(Prototype)
- Prototype 原型(创建型模式)
- 创建型模式:Prototype(原型)
- Prototype 原型(创建型模式) 笔记
- java原型模式(Prototype)-创建型
- 四、原型模式Prototype(创建型)
- 创建型:原型模式(Prototype)
- Prototype原型(创建型模式)
- Prototype原型(创建型模式)
- 创建型-原型模式(prototype)
- 设计模式:创建型模式:原型模式(prototype pattern)
- Prototype - 原型创建模式
- 设计模式(四)原型模式Prototype(创建型)
- 设计模式(四)原型模式Prototype(创建型)
- 设计模式(四)原型模式Prototype(创建型)
- 设计模式(四)原型模式(Prototype)-创建型
- 设计模式(四)原型模式Prototype(创建型)
- 设计模式(2)-创建型-原型模式(Prototype)
- 第三方路由器固件的简单介绍
- hdu 1811
- Chrome插件开发之CSDN博客阅读模式
- 题目85:求最大值
- apache 首页配置
- 创建型模式:Prototype(原型)
- 使用VNC远程连接服务器
- Oracle 10g 单实例数据库 Data Guard 之 Physical Standby 配置详解(根据官方文档总结)
- linux pppd配置流程
- centos 乱码问题处理
- 简单数据结构实现——堆栈
- This is a re-invention of protractor!
- Curl测试网页响应时间
- 创建型模式:Builder(生成器)