• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

prototype(原型)模式

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网

定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.
      Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,
      工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求
      原型对象拷贝它们自己来实施创建。
     
适用性:当要实例化的类是在运行时刻指定时,例如,通过动态装载;或者为了避免创建一个与产品类层次平行的工厂类层次时;或者当一个类的实例只能有几个不同状态组合中的一种时。  建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。
 
代码可以说明一切的:
代码部分
/*************************
 * 定义interface
 *************************/
 package meconsea;

/**
 *
 * <p>Title:design in java </p>
 *
 * <p>Description: 用Prototype来创建一批clone产品</p>
 * <p>有关clone的知识请参考另一个blog</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.hhzskj.com</p>
 *
 * @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;

/**
 *
 * <p>Title:design in java </p>
 *
 * <p>Description: 用Prototype来创建一批clone产品</p>
 * <p>有关clone的知识请参考另一个blog</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.hhzskj.com</p>
 *
 * @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;

/**
 *
 * <p>Title: 原型管理器 </p>
 *
 * <p>Description:实现了自动注册原型的功能 </p>
 * <p>原型管理器运行时只要一个实例,因此可以用singleton模式来实现.</p>
 * <p>有关singleton的参照其他blog.</p>
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company:www.hhzskj.com </p>
 *
 * @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;

/**
 *
 * <p>Title: </p>
 *
 * <p>Description: 客户端使用prototype模式</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.hhzskj.com</p>
 *
 * @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!

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

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网