版权声明:转载注明出处 https://blog.csdn.net/jy02268879/article/details/81638764
目录
浅复制
Sheep.java
import java.io.Serializable;
import java.util.Date;
/**
* 浅复制
*/
public class Sheep implements Cloneable,Serializable{
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
return obj;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Sheep() {
}
public Sheep(String sname, Date birthday) {
super();
this.sname = sname;
this.birthday = birthday;
}
}
client.java
package com.bjsxt.prototype;
import java.util.Date;
/**
* @author liyijie
* @email [email protected]
* @version
*/
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(14800000000L);
Sheep s1 = new Sheep("sheep1",date);
Sheep s2 = (Sheep) s1.clone();
System.out.println("s1:"+s1);
System.out.println("s1:"+s1.getSname());
System.out.println("s1:"+s1.getBirthday());
date.setTime(14888888888L);
System.out.println("s1:"+s1.getBirthday());
s2.setSname("sheep2");
System.out.println("s2:"+s2);
System.out.println("s2:"+s2.getSname());
//浅复制,S2引用的对象跟S1引用的是一个对象,s1的date对象修改后,s2的date对象也跟着修改
System.out.println("s2:"+s2.getBirthday());
}
}
深复制
Sheep2.java
import java.util.Date;
/**
* @author liyijie
* @date 2016��8��12������1:10:28
* @email [email protected]
* @remark 原型模式 深复制
* @version
*/
public class Sheep2 implements Cloneable{
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
//deep clone
Sheep2 s = (Sheep2)obj;
s.birthday = (Date) this.birthday.clone();//深复制
return obj;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Sheep2() {
}
public Sheep2(String sname, Date birthday) {
super();
this.sname = sname;
this.birthday = birthday;
}
}
Client2.java
import java.util.Date;
/**
* 深复制
* */
public class Client2 {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(14800000000L);
Sheep2 s1 = new Sheep2("sheep1",date);
Sheep2 s2 = (Sheep2) s1.clone();
System.out.println("s1:"+s1);
System.out.println("s1:"+s1.getSname());
System.out.println("s1:"+s1.getBirthday());
date.setTime(14888888888L);
System.out.println("s1:"+s1.getBirthday());
s2.setSname("sheep2");
System.out.println("s2:"+s2);
System.out.println("s2:"+s2.getSname());
System.out.println(s2.getBirthday());
}
}
用反序列化方式实现原型模式
Client3.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* @author liyijie
* @email [email protected]
* @remark 用反序列化方式实现原型模式
* @version
*/
public class Client3 {
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Date date = new Date(14800000000L);
Sheep s1 = new Sheep("sheep1",date);
System.out.println("s1:"+s1);
System.out.println("s1:"+s1.getSname());
System.out.println("s1:"+s1.getBirthday());
//用序列化/反序列化的方式实现深复制
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(s1);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
Sheep s2 = (Sheep) ois.readObject();
date.setTime(14888888888L);//ֵ
System.out.println("s1:"+s1.getBirthday());
s2.setSname("sheep2");
System.out.println("s2:"+s2);
System.out.println("s2:"+s2.getSname());
System.out.println(s2.getBirthday());
}
}