/**
* 克隆羊 多利 对象
*/
public class Sheep implements Cloneable{
private String name;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
//ֱ直接调用Object对象的clone()方法
Object obj=super.clone();
return obj;
}
public Sheep() {
}
public Sheep(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setName(String name) {
this.name = name;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
/**
* 原型模式------>浅复制(浅克隆)
*/
public class App {
public static void main(String[] args) throws CloneNotSupportedException {
Date date=new Date(123123123L);
Sheep s1=new Sheep("多利",date);
Sheep s2=(Sheep) s1.clone();
System.out.println(s1+"\t"+s1.getName()+"---->"+s1.getBirthday());
//这里更改后 s2 克隆也相应更改
date.setTime(321213543123L);
System.out.println(s1+"\t"+s1.getName()+"---->"+s1.getBirthday());
//可以更改s2的属性
s2.setName("多多利");
System.out.println(s2+"\t"+s2.getName()+"---->"+s2.getBirthday());
}
}
com.reyco.prototype.Sheep@7852e922 多利---->Fri Jan 02 18:12:03 CST 1970
com.reyco.prototype.Sheep@7852e922 多利---->Fri Mar 07 01:59:03 CST 1980
com.reyco.prototype.Sheep@55f96302 多多利---->Fri Mar 07 01:59:03 CST 1980
/**
* 原型模式------>深复制(深克隆)
*/
public class Sheep2 implements Cloneable{
private String name;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj=super.clone(); //直接调用Object对象的clone()方法
//添加以下代码实现深复制
Sheep2 s=(Sheep2) obj;
s.birthday=(Date) this.birthday.clone();
return obj;
}
public Sheep2() {
}
public Sheep2(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setName(String name) {
this.name = name;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
/**
* 原型模式------>深复制(深克隆)
* @author Administrator
*/
public class App2 {
public static void main(String[] args) throws CloneNotSupportedException {
Date date=new Date(123123123L);
Sheep2 s=new Sheep2("多利",date);
Sheep2 s2=(Sheep2) s.clone();
System.out.println(s.getName()+"---->"+s.getBirthday());
//这里更改后 s2克隆不会更改
date.setTime(321213543123L);
System.out.println(s.getName()+"---->"+s.getBirthday());
//可以更改s2的属性
s2.setName("多多利");
System.out.println(s2.getName()+"---->"+s2.getBirthday());
}
}
多利---->Fri Jan 02 18:12:03 CST 1970
多利---->Fri Mar 07 01:59:03 CST 1980
多多利---->Fri Jan 02 18:12:03 CST 1970
/**
* 序列化和反序列化实现深克隆必须实现Serializable接口
*/
public class Sheep implements Serializable{
private String name;
private Date birthday;
public Sheep() {
}
public Sheep(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setName(String name) {
this.name = name;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public static void main(String[] args) throws Exception {
Date date=new Date(123123123L);
Sheep s=new Sheep("多利",date);
System.out.println(s.getName()+"---->"+s.getBirthday());
//使用序列化和反序列化实现深复制
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(s);
byte[] bytes =bos.toByteArray();
ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
ObjectInputStream ois=new ObjectInputStream(bis);
Sheep s2=(Sheep) ois.readObject();
//这里更改后 s2克隆不会更改
date.setTime(321213543123L);
System.out.println(s.getName()+"---->"+s.getBirthday());
//可以更改s2的属性
s2.setName("多多利");
System.out.println(s2.getName()+"---->"+s2.getBirthday());
}
设计模式(1):介绍
设计模式(2):单例模式
设计模式(3):工厂模式
设计模式(4):建造者模式
设计模式(6):桥接模式
设计模式(7):装饰器模式
设计模式持续更新中…
更多【原型模式-设计模式(5):原型模式】相关视频教程:www.yxfzedu.com