原型模式(Prototype): 用原型实例指定创建对象的种类,并且通过拷贝原型来创建新的实例对象。它允许一个对象再创建另外一个可定制的对象,根本无需知道任何创建细节,工作原理,通过将一个原型对象传给那个要发动创建的对象,这个发动创建对象通过请求原型对象拷贝它们自己来创建。
通俗点,就是通过拷贝来进行创建实例。
结果:
For loop before ConcretePrototype name == this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!0 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!1 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!2 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!3 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!4 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!5 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!6 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!7 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!8 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!9 old this is propotype!
/////
原型模式注意深浅拷贝的问题
通俗点,就是通过拷贝来进行创建实例。
import java.util.HashMap; interface Prototype extends Cloneable { public void setName(String name); } class ConcretePrototype implements Prototype { private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public ConcretePrototype() { this.name = " this is propotype!"; } /** * * 覆写clone方法 * * * @return Object */ public Object clone() { Object o = null; try { o = super.clone(); } catch (CloneNotSupportedException e) { System.out.println(" PrototypeRam is not ConcretePrototype"); } return o; } } class PrototypeManager { /** * 关于HashMap和HashTable的区别参考其他blog */ private HashMap hm = null; private static PrototypeManager prototypeManager = null; private PrototypeManager() { hm = new HashMap(); } public static synchronized PrototypeManager getPrototypeManager() { if (prototypeManager == null) { prototypeManager = new PrototypeManager(); } return prototypeManager; } /** * * 注册 * * * @param name * String * * @param prototype * Object */ public void register(String name, Object prototype) { hm.put(name, prototype); } /** * * 解除注册 * * * @param name * String */ public void unRegister(String name) { hm.remove(name); } /** * * 获得原型实例 * * * @param name * String * * @return Object */ public Object getPrototype(String name) { Object o = null; if (hm.containsKey(name)) { o = hm.get(name); } else { try { /** * 自动查找原型管理器里不存在的类,并动态生成他 */ o = Class.forName(name).newInstance(); this.register(name, o); } catch (Exception e) { System.out.println("class " + name + " don't define " + e.getMessage()); e.printStackTrace(); } } return o; } } /** * * * * <p> * Title: * </p> * * * <p> * Description: 客户端使用prototype模式 * </p> * * * <p> * Copyright: Copyright (c) 2008 * </p> * * * * @author meconsea * * @version 1.0 */ public class PrototypeDemo { PrototypeManager pm = null; public PrototypeDemo() { pm = PrototypeManager.getPrototypeManager(); } public static void main(String args[]) { PrototypeDemo pc = new PrototypeDemo(); String className = null; className = "ConcretePrototype"; ConcretePrototype pr = null; pr = (ConcretePrototype) (pc.pm.getPrototype(className)); if (pr != null) { ConcretePrototype[] pram; System.out.println("For loop before ConcretePrototype name == " + pr.getName()); pram = new ConcretePrototype[10]; for (int i = 0; i < 10; i++) { /** * 生成一批克隆的对象,并比较他们和原型的不同 */ pram[i] = (ConcretePrototype) pr.clone(); System.out.println("ConcretePrototype name == " + pram[i].getName()); pram[i].setName(pram[i].getName() + i); System.out.println("clone changes after " + pram[i].getName() + " old " + pr.getName()); } } } }
结果:
For loop before ConcretePrototype name == this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!0 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!1 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!2 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!3 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!4 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!5 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!6 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!7 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!8 old this is propotype!
ConcretePrototype name == this is propotype!
clone changes after this is propotype!9 old this is propotype!
/////
原型模式注意深浅拷贝的问题
发表评论
-
【转】Chain of Responsibility模式(职责链)
2009-05-09 17:01 777Java深入到一定程度,就不可避免的碰到设计模式这一概念,了解 ... -
【转】Chain of Responsibility(职责链)
2009-05-09 17:00 723Chain of Responsibility定义 Chain ... -
装饰器模式
2009-05-09 16:11 735【转】http://www.iteye.com/t ... -
【转】策略模式
2009-05-05 15:57 633【策略模式应用场景举例】 比如在玩“极品飞车”这款游 ... -
转 桥梁模式
2009-04-23 10:43 804桥梁模式的用意是将问 ... -
转 Adapter适配器模式
2009-04-23 10:39 631GOF《设计模式》一书对Adapter模式是这样描述的: ... -
单态模式
2009-04-22 15:11 1066单态模式限制了类实例的创建,但采用这种模式设计的类,可以保证仅 ... -
Java代理模式<转>
2009-04-21 16:32 867.代理模式 代理模式的 ... -
Java工厂模式<转>
2009-04-21 15:58 1245看了这么多关于工厂模 ...
相关推荐
设计模式之原型模式prototype有更多需要请联系我wyb_jany@126.com
即原型模式,提供一个已经存在的对象进行新对象创建的接口,一般情况下都是使用Clone接口。 此模式非常简单,简单的说就是复制多个当前对象供使用。Prototype模式允许一个对象再创建另外一个可定制的对象,根本...
设计模式C++学习之原型模式(Prototype)
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
6、原型模式(Prototype Pattern) 用意:通过拷贝创建一个新的对象
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/1032892
原型模式的示例代码和文档,学习原型模式的参考资料。
一个原型设计模式的应用案例,C#语言编写,有助于设计模式的理解和学以致用。
原型模式是一种创建型设计模式,它通过复制一个现有的对象来创建新的对象,而不是通过调用构造函数的方式。这种方式可以在运行时动态地创建和修改对象,而不需要知道具体的创建细节 。 原型模式的基本概念包括以下...
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
主要介绍了PHP设计模式:原型模式Prototype,结合实例形式详细分析了PHP原型模式Prototype的基本概念、功能、原理、实现方法与操作注意事项,需要的朋友可以参考下
原型模式(Prototype Pattern)作为创建型模式的一种,通过复制现有对象来创建新对象,从而避免了使用构造函数直接实例化对象的过程。
C#面向对象设计模式纵横谈(6):Prototype 原型模式(创建型模式) 体验课程
二、原型模式结构图 三、原型模式中主要角色 抽象原型(Prototype)角色:声明一个克隆自身的接口 具体原型(Concrete Prototype)角色:实现一个克隆自身的操作 四、原型模式的优点和缺点 Prototype模式优点: 1、可以...
概述 原型模式是指原型实例指向创建对象的种类,并通过拷贝这些原型创建新的对象,是一种用来创建对象的模式,也就是创建一个对象作为另一个对象的prototype属性; prototype警告:学习了解原型模式前需先学习...
C#面向对象设计模式纵横谈(6):Prototype 原型模式(创建型模式) (Level 300)
IOS设计模式浅析之原型模式(Prototype)--copy - iOS知识库1
C#面向对象设计模式纵横谈(6):Prototype 原型模式(创建型模式)