欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

原型模式(Prototype):

程序员文章站 2022-06-12 21:42:00
...
原型模式(Prototype): 用原型实例指定创建对象的种类,并且通过拷贝原型来创建新的实例对象。它允许一个对象再创建另外一个可定制的对象,根本无需知道任何创建细节,工作原理,通过将一个原型对象传给那个要发动创建的对象,这个发动创建对象通过请求原型对象拷贝它们自己来创建。

通俗点,就是通过拷贝来进行创建实例。

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!
/////
原型模式注意深浅拷贝的问题