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());
}
}
}
}
分享到:
相关推荐
该代码是android应用设计模式之原型模式小例子
设计模式之原型模式的Java版本实现和UML类设计图
iOS设计模式之原型模式 博客地址:http://blog.csdn.net/ioswyl88219/article/details/17662945
java设计模式之原型模式,希望对大家有所帮助。
设计模式
设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) 结构模式: 设计模式之Facade(外观) 设计模式之Proxy(代理) 设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator...
Java 经典设计模式讲解以及项目实战 设计模式简介:主要介绍各种设计模式的概念和运用场景等 设计模式综合运用:主要是笔者在实际工作中运用到的一些设计模式综合运用事例的提炼 Spring设计模式简介:主要是讲述...
iOS设计模式之原型设计模式
设计模式的原型模式的例子,希望对大家有用~~~~~~~~
章节介绍:1、爪哇语言结构性模式之变压器模式介绍 2、爪哇语言抽象工厂创立性模式介绍 3、工厂方法创立...10、设计模式之State 11、设计模式之Facade(外观) 12、设计模式之Interpreter(解释器) 13、设计模式之Visitor
设计模式之禅之 原型模式 设计模式之禅之 原型模式 设计模式之禅之 原型模式设计模式之禅之 原型模式