原型模式
概念
原型模式:将给出的原型对象,复制一份出来。
复制的模式分两种,一种是浅克隆只复制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());
}
}