• 设计模式----原型模式


    一、模式定义

      所谓原型模式就是用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。

      能够克隆的类需要实现Cloneable接口,且提供clone()接口。

      当通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。

      其中克隆又分为:

          深克隆:

            1.普通实现

            2.利用序列化和反序列化实现

          浅克隆。

    二、代码实现

      其中Sheep为能够被克隆的类

     1 import java.io.Serializable;
     2 import java.util.Date;
     3 
     4 public class Sheep implements Cloneable,Serializable{
     5     private String sname;
     6     private Date birthday;
     7     
     8     @Override
     9     protected Object clone() throws CloneNotSupportedException {
    10         // TODO Auto-generated method stub
    11         Object obj = super.clone();
    12         
    13         return obj;
    14     }
    15     
    16     public Sheep() {
    17         super();
    18     }
    19 
    20 
    21 
    22     public String getSname() {
    23         return sname;
    24     }
    25 
    26 
    27 
    28     public void setSname(String sname) {
    29         this.sname = sname;
    30     }
    31 
    32 
    33 
    34     public Date getBirthday() {
    35         return birthday;
    36     }
    37 
    38 
    39 
    40     public void setBirthday(Date birthday) {
    41         this.birthday = birthday;
    42     }
    43 
    44 
    45 
    46     public Sheep(String sname, Date birthday) {
    47         super();
    48         this.sname = sname;
    49         this.birthday = birthday;
    50     }
    51 }

    下面为使用过程:

     1 public class Client4 {
     2     public static void main(String[] args) throws CloneNotSupportedException {
     3         testNew(100);
     4         testClone(100);
     5     }
     6 
     7     public static void testNew(int size){
     8         long start = System.currentTimeMillis();
     9         for(int i=0;i<size;i++){
    10             Laptop t = new Laptop();
    11         }
    12         long end = System.currentTimeMillis();
    13         System.out.println("以new的方式创建耗时:"+(end-start));
    14     }
    15     
    16     public static void testClone(int size) throws CloneNotSupportedException{
    17         long start = System.currentTimeMillis();
    18         Laptop t = new Laptop();
    19         for(int i=0;i<size;i++){
    20             Laptop temp = (Laptop)t.clone();
    21         }
    22         long end = System.currentTimeMillis();
    23         System.out.println("以Clone的方式创建耗时:"+(end-start));
    24     }
    25 }
    26 
    27 class Laptop implements Cloneable{
    28     public Laptop(){
    29         try {
    30             Thread.sleep(10);
    31         } catch (InterruptedException e) {
    32             // TODO Auto-generated catch block
    33             e.printStackTrace();
    34         }
    35     }
    36     
    37     protected Object clone() throws CloneNotSupportedException{
    38         Object obj = super.clone();
    39         return obj;
    40     }
    41 }

    结果如下:

    可以看出,当new产生一个对象需要非常繁琐的数据准备或访问权限时,使用克隆可以大大的提高效率。

    三、开发中的应用场景

      原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂方法提供给调用者。

  • 相关阅读:
    java将pdf转成base64字符串及将base64字符串反转pdf
    input校验不能以0开头的数字
    js校验密码,不能为空的8-20位非纯数字或字母的密码
    tomcat正常关闭,端口号占用解决 StandardServer.await: create[8005]:
    Eclipse中项目报Target runtime com.genuitec.runtime.generic.jee60 is not defined异常的解决
    Access restriction: The type Base64 is not accessible due to restriction on
    [操作系统] 线程和进程的简单解释
    ssh登录一段时间后断开的解决方案
    [SAMtools] 常用指令总结
    [C] 有关内存问题
  • 原文地址:https://www.cnblogs.com/blzm742624643/p/9962080.html
Copyright © 2020-2023  润新知