Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 269|回复: 0

[设计模式学习]prototype(原型)模式

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:57:20 | 显示全部楼层 |阅读模式
    定义:
         用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.  

          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); } /&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;
      &times; 接口的实现
      &times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;/
      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;
       }
    } /&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;
      &times;原型管理器部分
      &times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;/
      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;
       }
    }
    /&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;
      &times;客户端实现
      &times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;&times;/
      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 = (PrototypeRam)pr.clone();
             System.out.println("loop for PrototypeRam name == "+pram.getName());
             pram.setName(pram.getName()+i);
             System.out.println("clone changes after "+pram.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!  /*******************************
      *代码说明了一切吧!^…^
      *******************************/
      
       
         
         
          
          

            
          

          
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/28/235720640.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:29 , Processed in 0.302247 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表