站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题prototype(原型)模式

prototype(原型)模式

摘要: prototype(原型)模式

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="535" height="86" align="left" valign="top">

定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.
Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,
工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求
原型对象拷贝它们自己来实施创建。

适用性:当要实例化的类是在运行时刻指定时,例如,通过动态装载;或者为了避免创建一个与产品类层次平行的工厂类层次时;或者当一个类的实例只能有几个不同状态组合中的一种时。 建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。

代码可以说明一切的:
代码部分
/*************************
* 定义interface
*************************/
package meconsea;

/**
*
*

Title:design in java


*
*

Description: 用Prototype来创建一批clone产品


*

有关clone的知识请参考另一个blog


*
*

Copyright: Copyright (c) 2005


*
*

Company: www.hhzskj.com


*
* @author meconsea
* @version 1.0
*/

import java.io.Serializable;

public interface IPrototypeRam extends Cloneable, Serializable{

/**
* 用于bean
* @return String
*/
public String getName();

/**
* 用于bean
* @param name String
*/

public void setName(String name);

}

/×××××××××××××××××××××××××××××××××××
× 接口的实现
×××××××××××××××××××××××××××××××××××/
package meconsea;

/**
*
*

Title:design in java


*
*

Description: 用Prototype来创建一批clone产品


*

有关clone的知识请参考另一个blog


*
*

Copyright: Copyright (c) 2005


*
*

Company: www.hhzskj.com


*
* @author meconsea
* @version 1.0
*/

public class PrototypeRam implements IPrototypeRam{

private String name;

public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}

public PrototypeRam() {
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 cloneable");
}
return o;
}
}

/×××××××××××××××××××××××××××××××××××××××
×原型管理器部分
×××××××××××××××××××××××××××××××××××××××/
package meconsea;

/**
*
*

Title: 原型管理器


*
*

Description:实现了自动注册原型的功能


*

原型管理器运行时只要一个实例,因此可以用singleton模式来实现.


*

有关singleton的参照其他blog.


*

Copyright: Copyright (c) 2005


*
*

Company:www.hhzskj.com


*
* @author meconsea
* @version 1.0
*/
import java.util.HashMap;

public 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;
}
}


/××××××××××××××××××××××××××××××
×客户端实现
×××××××××××××××××××××××××××××/
package meconsea;

/**
*
*

Title:


*
*

Description: 客户端使用prototype模式


*
*

Copyright: Copyright (c) 2005


*
*

Company: www.hhzskj.com


*
* @author meconsea
* @version 1.0
*/

public class PrototypeClient {
PrototypeManager pm = null;
public PrototypeClient() {
pm = PrototypeManager.getPrototypeManager();
}
public static void main(String args[]){
PrototypeClient pc = new PrototypeClient();
String className = null;
className = "meconsea.PrototypeRam";
PrototypeRam pr = null;
pr = (PrototypeRam)(pc.pm.getPrototype(className));
if(pr != null){
PrototypeRam[] pram;
System.out.println(" for loop before PrototypeRam name == "+pr.getName());
pram = new PrototypeRam[10];
for(int i = 0; i < 10; i++){
/**
* 生成一批克隆的Ram,并比较他们和原型的不同
*/
pram[i] = (PrototypeRam)pr.clone();
System.out.println("loop for PrototypeRam name == "+pram[i].getName());
pram[i].setName(pram[i].getName()+i);
System.out.println("clone changes after "+pram[i].getName()+" old "+pr.getName());
}
}
}
}

运行结果:

C:\java>java PrototypeClient
for loop before PrototypeRam name == this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!0 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!1 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!2 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!3 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!4 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!5 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!6 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!7 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!8 old this is propotype!
loop for PrototypeRam name == this is propotype!
clone changes after this is propotype!9 old this is propotype!

/*******************************
*代码说明了一切吧!^…^
*******************************/

</td> <td width="165" valign="top"> </td> </tr> </table> </td> </tr> <tr>


↑返回目录
前一篇: 使用state pattern替代if else
后一篇: 列出当前系统的硬盘盘符