設計模式(五)---- 原型模式
原型模式
通過 new 產生一個對象需要非常繁瑣的數據準備或訪問權限,則可以使用原型模式。就是 java 中的克隆技術,以某個對象為原型,復制出新的對象。顯然,新的對象具備原型對象的特點。
優勢:創建對象效率高(直接克隆,避免重新執行構造過程步驟)
克隆類似於 new 但是不同於 new。new 創建新的對象,屬性采用默認值;克隆的對象屬性值和原型對象相同。並且克隆出的新對象改變不會影響到原型對象。然後,再修改克隆對象的值。
原型模式實現
實現 Cloneable 接口,重寫 clone 方法;
Prototype 模式中實現最困難的地方就是內存復制操作,所幸在 Java 中提供了 clone() 方法替我們做了絕大部分事情。
1、淺拷貝實現:
public class Sheep implements Cloneable
{
private String sname;
private Date birthday;
public Sheep()
{
}
public Sheep(String sname, Date birthday)
{
this.sname = sname;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException
{
Object object = super.clone();
return object;
}
//省略 getter 和 setter 方法
}
測試類,如下:
public class Client
{
public static void main(String[] args) throws CloneNotSupportedException
{
Date date = new Date(111111111111L);
Sheep sheep1 = new Sheep("多利", date);
System.out.println(sheep1);
System.out.println("sheep1: " + sheep1.getSname() + ":" + sheep1.getBirthday());
System.out.println("------------------------------");
Sheep sheep2 = (Sheep) sheep1.clone();
System.out.println(sheep2);
System.out.println("sheep2: " + sheep2.getSname() + ":" + sheep2.getBirthday());
System.out.println("----改變date後---");
date.setTime(1235313478966L); //改變date對象的值
System.out.println("sheep2: " + sheep2.getSname() + ":" + sheep2.getBirthday());
}
}
結果如下:
com.yufeng.prototype.Sheep@60e53b93
sheep1: 多利:Tue Jul 10 08:11:51 CST 1973
------------------------------
com.yufeng.prototype.Sheep@5e481248
sheep2: 多利:Tue Jul 10 08:11:51 CST 1973
----改變date後---
sheep2: 多利:Sun Feb 22 22:37:58 CST 2009
結果分析:
通過sheep1的clone()方法克隆的對象 sheep2,接著改變date對象的值之後,sheep2 對象中 birthday 發生了變化;說明 sheep1 和 sheep2 指向了同一個 date 對象,所以為淺拷貝;
2、深拷貝實現:
方式一:屬性也進行clone
實現 Cloneable 接口,重寫clone()方法
public class Sheep implements Cloneable
{
private String sname;
private Date birthday;
public Sheep()
{
}
public Sheep(String sname, Date birthday)
{
this.sname = sname;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException
{
Object object = super.clone();
//添加如下代碼實現深拷貝(deep clone)
Sheep sheep = (Sheep) object;
sheep.setBirthday((Date) this.birthday.clone());
return object;
}
//省略 getter 和 setter 方法
}
結果如下:
com.yufeng.prototype.Sheep@60e53b93
sheep1: 多利:Tue Jul 10 08:11:51 CST 1973
------------------------------
com.yufeng.prototype.Sheep@5e481248
sheep2: 多利:Tue Jul 10 08:11:51 CST 1973
----改變date後---
sheep2: 多利:Tue Jul 10 08:11:51 CST 1973
方式二:序列化和反序列化實現深拷貝
實現 Cloneable 接口,重寫clone()方法; 實現 Serializable 接口
public class Sheep implements Cloneable, Serializable
{
private String sname;
private Date birthday;
public Sheep()
{
}
public Sheep(String sname, Date birthday)
{
this.sname = sname;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException
{
Object object = super.clone();
return object;
}
// 省略 getter 和 setter 方法
}
測試類,代碼如下:
public class Client2
{
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException
{
Date date = new Date(111111111111L);
Sheep sheep1 = new Sheep("多利", date);
System.out.println(sheep1);
System.out.println("sheep1: " + sheep1.getSname() + ":" + sheep1.getBirthday());
System.out.println("------------------------------");
//Sheep sheep2 = (Sheep) sheep1.clone();
//使用序列化和反序列化實現深克隆
//序列化到字節數組中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(sheep1);
byte[] bytes = bos.toByteArray();
//反序列化
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
Sheep sheep2 = (Sheep) ois.readObject();
System.out.println(sheep2);
System.out.println("sheep2: " + sheep2.getSname() + ":" + sheep2.getBirthday());
System.out.println("----改變date後---");
date.setTime(1235313478966L);
System.out.println("sheep2: " + sheep2.getSname() + ":" + sheep2.getBirthday());
}
}
結果如下:
com.yufeng.prototype.Sheep@60e53b93
sheep1: 多利:Tue Jul 10 08:11:51 CST 1973
------------------------------
com.yufeng.prototype.Sheep@5e481248
sheep2: 多利:Tue Jul 10 08:11:51 CST 1973
----改變date後---
sheep2: 多利:Tue Jul 10 08:11:51 CST 1973
設計模式(五)---- 原型模式