/**
* Sheep类,测试原型模式之浅拷贝
* 时间:2015年4月1日09:44:29
*/
package com.bjsxt.cn.prototype;
import java.io.Serializable;
import java.util.Date;
public class Sheep implements Cloneable, Serializable {
private String name;
private Date date;
public Sheep() {
}
public Sheep(String name, Date date) {
super();
this.name = name;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
protected Object clone() throws CloneNotSupportedException {//通过这个方法的两行代码实现浅克隆
Object object = super.clone();
return object;
}
}
/**
* Sheep类,测试原型模式之浅拷贝
* 时间:2015年4月1日09:44:29
*/
package com.bjsxt.cn.prototype;
import java.util.Date;
public class Sheep2 implements Cloneable {
private String name;
private Date date;
public Sheep2() {
}
public Sheep2(String name, Date date) {
super();
this.name = name;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object object = super.clone();
//通过增加的这几行实现深复制
Sheep2 sheep = (Sheep2) object;
sheep.date = (Date) this.date.clone();
return object;
}
}
/**
* 测试浅克隆
* 时间:2015年4月1日09:48:21
* 程序理解:该小程序测试了圆形模式浅克隆的相关问题。对比程序和运行结果,我们可以看出以下几点:
* 1, 在修改之后,确实实现了复制,并且s1和s2的作为对象,所指向的属性值完全相同
* 2, s1和s2作为局部变量,值并不一样。并且根据打印的结果,它们都属于Sheep类型
* 3, s1和s2为Sheep对象,该类具有Date属性,但是s1和s2的date属性指向了同样的内存空间。这可以通过
* 我们修改了date的值以后,s1和s2的属性date都跟着改变了。显然,在某些情况下,这不符合我们的要求,因为
* s1和s2还有粘连。我们希望它们能彼此独立。修改一个对象,与另一个对象无关。当然这就是浅拷贝的问题所在了
*
*/
package com.bjsxt.cn.prototype;
import java.util.Date;
public class Client1 {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(3234324435L);
Sheep s1 = new Sheep("少莉", date);
Sheep s2 = (Sheep) s1.clone();
System.out.println("----------修改之前------------");
System.out.println(s1.getClass());
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getDate());
System.out.println(s2.getClass());
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getDate());
System.out.println("----------修改-----------");
date.setTime(4323423423L);
System.out.println("----------修改之后-------------");
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getDate());
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getDate());
}
}
/*
* ----------修改之前------------
com.bjsxt.cn.prototype.Sheep@659e0bfd
少莉
Sat Feb 07 18:25:24 CST 1970
com.bjsxt.cn.prototype.Sheep@4e25154f
少莉
Sat Feb 07 18:25:24 CST 1970
----------修改-----------
----------修改之后-------------
com.bjsxt.cn.prototype.Sheep@659e0bfd
少莉
Fri Feb 20 08:57:03 CST 1970
com.bjsxt.cn.prototype.Sheep@4e25154f
少莉
Fri Feb 20 08:57:03 CST 1970
*
* */
/**
* 测试深拷贝
* 时间:2015年4月1日09:48:21
* 程序理解:该小程序测试了原型模式深克隆的相关问题。对比程序和运行结果,我们可以看出以下几点:
* 通过程序,我们可以看出,在Sheep2类中的clone方法中增加了几行语句
* @Override
protected Object clone() throws CloneNotSupportedException {
Object object = super.clone();
//通过增加的这几行实现深复制
Sheep2 sheep = (Sheep2) object;
sheep.date = (Date) this.date.clone();
return object;
}
就实现了深拷贝,这是通过我们对属性也进行了深拷贝实现的。
*/
package com.bjsxt.cn.prototype;
import java.util.Date;
public class Client2 {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(3234324435L);
String name = new String("少莉");
Sheep2 s1 = new Sheep2(name, date);
Sheep2 s2 = (Sheep2) s1.clone();
System.out.println("----------修改之前------------");
System.out.println("s1-------------");
System.out.println(s1.getClass());
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getDate());
System.out.println();
System.out.println("s2-------------");
System.out.println(s2.getClass());
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getDate());
System.out.println("----------修改-----------");
date.setTime(43234234238989L);
s1.setName("周杰伦");
System.out.println("----------修改之后-------------");
System.out.println("s1-------------");
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getDate());
System.out.println();
System.out.println("s2-------------");
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getDate());
}
}
/*
----------修改之前------------
s1-------------
class com.bjsxt.cn.prototype.Sheep2
com.bjsxt.cn.prototype.Sheep2@659e0bfd
少莉
Sat Feb 07 18:25:24 CST 1970
s2-------------
class com.bjsxt.cn.prototype.Sheep2
com.bjsxt.cn.prototype.Sheep2@4e25154f
少莉
Sat Feb 07 18:25:24 CST 1970
----------修改-----------
----------修改之后-------------
s1-------------
com.bjsxt.cn.prototype.Sheep2@659e0bfd
周杰伦
Fri Jan 15 13:30:38 CST 3340
s2-------------
com.bjsxt.cn.prototype.Sheep2@4e25154f
少莉
Sat Feb 07 18:25:24 CST 1970
*
* */
/**
* 测试使用序列化和反序列化实现深拷贝。
* 时间:2015年4月1日10:27:57
* 注意:记住序列化的过程。
*/
package com.bjsxt.cn.prototype;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class Client3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Date date = new Date(3234324435L);
Sheep s1 = new Sheep("少莉", date);
//把这个对象写入到内存中的数组。通过使用字节数组输出流baos获取内存中的一块内存
//外加一个处理流,方便操作,多态的存在,因此必须使用ObjectOutputStream类来声明。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(s1);
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
Sheep s2 = (Sheep) ois.readObject();
System.out.println("----------修改之前------------");
System.out.println("s1-------------");
System.out.println(s1.getClass());
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getDate());
System.out.println();
System.out.println("s2-------------");
System.out.println(s2.getClass());
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getDate());
System.out.println("----------修改-----------");
date.setTime(43234234238989L);
s1.setName("周杰伦");
System.out.println("----------修改之后-------------");
System.out.println("s1-------------");
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getDate());
System.out.println();
System.out.println("s2-------------");
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getDate());
}
}
/**
*
* ----------修改之前------------
s1-------------
class com.bjsxt.cn.prototype.Sheep
com.bjsxt.cn.prototype.Sheep@33909752
少莉
Sat Feb 07 18:25:24 CST 1970
s2-------------
class com.bjsxt.cn.prototype.Sheep
com.bjsxt.cn.prototype.Sheep@1540e19d
少莉
Sat Feb 07 18:25:24 CST 1970
----------修改-----------
----------修改之后-------------
s1-------------
com.bjsxt.cn.prototype.Sheep@33909752
周杰伦
Fri Jan 15 13:30:38 CST 3340
s2-------------
com.bjsxt.cn.prototype.Sheep@1540e19d
少莉
Sat Feb 07 18:25:24 CST 1970
*/
/**
* 测试new方法和clone方法的效率比较
* 通过运行结果我们可以看出,clone极大的提高了创建对象的效率。
*
*/
package com.bjsxt.cn.prototype;
import java.util.Date;
public class Client4 {
public static void testNew(int size) {
long start = System.currentTimeMillis();
for (int i=0; i<size; i++) {
Laptop tempLaptop = new Laptop();
}
long end = System.currentTimeMillis();
System.out.println("New耗时: " + (end-start));
}
public static void testClone(int size) throws CloneNotSupportedException {
long start = System.currentTimeMillis();
Laptop t = new Laptop();
for (int i=0; i<size-1; i++) {
Laptop temp = (Laptop) t.clone();
}
long end = System.currentTimeMillis();
System.out.println("Clone耗时: " + (end-start));
}
public static void main(String[] args) throws CloneNotSupportedException {
testNew(1000);
testClone(1000);
}
}
class Laptop implements Cloneable{
public Laptop() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
//通过增加的这几行实现浅复制
Object object = super.clone();
return object;
}
}
/*
* New耗时: 10550
Clone耗时: 10
*
*/