论坛首页 入门技术论坛

设计模式之原型模式

浏览 1511 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-07-27  
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());
			}
		}
	}
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics