克隆模式的核心:通过new产生一个新对象需要非常繁琐的数据对象和和访问权限,则可以利用原型模式。克隆类似于new对象,但是于new对象完全不同,clone对象是一个于原来对象属性完全一样的对象。
浅克隆的一种方式
sheep方法
package com.dasenlin.cn;
import java.util.Date;
/**
* 羊的实体类
* @author Administrator
* 若要使用clone方法,得先要集成Cloneable方法
*/
public class Sheep implements Cloneable {
private String name;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj =super.clone();//浅克隆
return obj;
}
public Sheep() {
super();
}
public Sheep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
深克隆的一种方法
package com.dasenlin.cn;
import java.util.Date;
/**
* 测试深克隆
* @author Administrator
*
*/
public class Sheep2 implements Cloneable {
private String name;
private Date birthday;
public Sheep2() {
super();
}
public Sheep2(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj=super.clone();
//添加如下代码实现深复制
Sheep2 s=(Sheep2)obj;
s.birthday=(Date)this.birthday.clone();//把属性也进行克隆
return obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
测试的方法
package com.dasenlin.cn;
import java.util.Date;
/**
* 测试Object的clone的方法(浅克隆测试)
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
try {
Sheep s1 =new Sheep("多利的来源体",new Date(789524687952L));
Sheep s2=(Sheep)s1.clone();
System.out.println(s1.getBirthday());
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
s1.setBirthday(new Date(18769523685789L));
System.out.println(s1.getBirthday()+"修改了的时间");
System.out.println(s2);
s2.setName("多利");
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
深克隆测试
package com.dasenlin.cn;
import java.util.Date;
/**
* 测试Object的clone的方法(深克隆测试)
* @author Administrator
*
*/
public class Client2 {
public static void main(String[] args) {
try {
Sheep2 s1 = new Sheep2("多利的来源体",new Date(789524687952L));
Sheep2 s2 = (Sheep2)s1.clone();
System.out.println(s1.getBirthday());
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
s1.setBirthday(new Date(18769523685789L));
System.out.println(s1.getBirthday()+"修改了的时间");
System.out.println(s2);
s2.setName("多利");
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
实现深克隆的另外一种方式,序列化和反序列化
实现深克隆序列化的实体类
package com.dasenlin.cn;
import java.util.Date;
/**
* 羊的实体类
* @author Administrator
* 若要使用clone方法,得先要集成Cloneable方法
*/
public class Sheep implements Cloneable,Serializable {
private String name;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj =super.clone();//浅克隆
return obj;
}
public Sheep() {
super();
}
public Sheep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
测试代码
public class Client3 {
public static void main(String[] args) {
try {
Sheep s1 =new Sheep("多利的来源体",new Date(789524687952L));
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(s1);
byte[]temp = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(temp);
ObjectInputStream ois = new ObjectInputStream(bis);
Sheep s2 =(Sheep)ois.readObject();
}catch(IOException e){
e.printStrack();
}finally{
CloseUtil(bis,ois,);
}
s1.setBirthday(new Date(18769523685789L));
System.out.println(s1.getBirthday()+"修改了的时间");
System.out.println(s2);
s2.setName("多利");
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}