设计模式之原型模式

原型模式

概念

原型模式:将给出的原型对象,复制一份出来。
复制的模式分两种,一种是浅克隆只复制Java基础类型,一种是深克隆能够复制引用类型

源码
原型源码
package prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @author :  freedom
 * @Description :  原型类 实现克隆空接口
 * @Creation Date:  2019-12-20 7:44 上午
 */
public class Daily implements Cloneable, Serializable {
  private String userName;
  private String dateTime;
  private Attachment attachment;


  public String getUserName() {
    return userName;
  }

  public Attachment getAttachment() {
    return attachment;
  }

  public void setAttachment(Attachment attachment) {
    this.attachment = attachment;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getDateTime() {
    return dateTime;
  }

  public void setDateTime(String dateTime) {
    this.dateTime = dateTime;
  }

  /**
   * 浅拷贝
   * @return
   */
  @Override
  public Daily clone(){
    Object obj=null;
    if(obj==null){
      try {
        /**
         * 调用父类的克隆方法
         */
        obj=super.clone();
      } catch (CloneNotSupportedException e) {
        e.printStackTrace();
      }
    }
    return (Daily) obj;
  }

  /**
   * 深拷贝 在内存里完成对象的拷贝
   * @return
   */
  public Daily deepClone(){
    /**
     * 写入对象到内存
     */
    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
    try {
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
      objectOutputStream.writeObject( this);
      /**
       * 从内存读取对象
       */
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
      try {
        return (Daily) objectInputStream.readObject();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }
}
原型附件引用类
package prototype;

import java.io.Serializable;

/**
 * @author :  freedom
 * @Description :  附件类,实现空序列化接口
 * @Creation Date:  2019-12-20 9:21 上午
 */
public class Attachment implements Serializable {

  private String fileName;
  private String content;

  public String getFileName() {
    return fileName;
  }

  public void setFileName(String fileName) {
    this.fileName = fileName;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }
}

原型模式测试类
package prototype;

/**
 * @author :  freedom
 * @Description :  原型模式测试类
 * @Creation Date:  2019-12-20 7:53 上午
 */
public class DailyTest {

  public static void main(String[] args) {

    /**
     * 浅克隆测试
     */
    Daily old_daily= new Daily();
    old_daily.setUserName("张三");
    old_daily.setDateTime("2018-12-11");
    Attachment attachment= new Attachment();
    attachment.setFileName("日报附件");
    old_daily.setAttachment(attachment);
    Daily new_daily=old_daily.clone();
    System.out.println(new_daily.getDateTime());
    System.out.println(new_daily.getUserName());
    System.out.println(old_daily==new_daily);
    /**
     * 浅拷贝引用对象是同一个
     */
    System.out.println(new_daily.getAttachment()==old_daily.getAttachment());

    Daily deep_daily= new Daily();
    /**
     * 深拷贝,引用类型也会被完全拷贝
     */
    System.out.println(deep_daily.getAttachment()==old_daily.getAttachment());

  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bigdatafreedom

你的鼓励奖是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值