原型模式
通過new產生一個對象需要非常繁瑣的數據準備或者訪問權限,可以使用原型模式。原型模式就是Java中的克隆技術,以某個對象爲原型,複製出新的對象。顯然,新的對象具備原型模式的特點。原型模式的優勢:效率高(直接克隆,避免了重新執行構造過程步驟)。
克隆和new類似,但是不同於new。new創建新的對象屬性用的是默認值,克隆出的對象的屬性值完全和原型對象相同。並且克隆出的新對象改變不會影響原型對象。然後,再修改克隆對象的值。
Prototype模式中實現起來最困難的地方就是內存複製操作,所幸在Java中提供了clone()方法替我們做了絕大一部分事情,這裏需要注意的是,clone()方法是Object裏面的,但是如果需要克隆類的對象,必須實現Cloneable接口。
原型模式深複製和淺複製demo
我們模仿克隆羊多利,實現一下羊的克隆,代碼如下:
public class Sheep implements Cloneable{
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
//直接調用Object對象的clone()方法
Object obj = super.clone();
//添加以下代碼實現深複製(deep clone)
/*Sheep s = (Sheep) obj;
s.birthday = (Date)this.birthday.clone();*/
return obj;
}
public Sheep(String sname, Date birthday) {
this.sname = sname;
this.birthday = birthday;
}
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 class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(123123123L);
Sheep sheep = new Sheep("小羊", date);
Sheep s2 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep.getSname());
System.out.println(sheep.getBirthday());
s2.setSname("小小小小羊");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
執行結果如下:
可以看到成功克隆了一個“小小小小羊”對象出來。
但是這樣的複製存在問題,如果我們在克隆出了“小小小小羊”對象之後,修改“小羊”對象的生日,那麼“小小小小羊”對象的生日也將被修改,我們修改代碼如下:
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(123123123L);
Sheep sheep = new Sheep("小羊", date);
Sheep s2 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep.getSname());
System.out.println(sheep.getBirthday());
//修改"小羊"的生日
date.setTime(12345123L);
System.out.println(sheep.getBirthday());
s2.setSname("小小小小羊");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
運行結果如下所示:
我們稱這樣的複製爲淺複製,出現這樣的問題的原因是,克隆出的對象和原對象指向同一個data對象,淺複製對應的是深複製,深複製出來的對象和原對象指向不同的data對象。
我們將Sheep類中註釋的兩行代碼解開註釋,然後再次運行上面的代碼,則可以得到如下結果:
可以看到,在我們實現了深複製之後,在克隆出了“小小小小羊”對象之後,修改“小羊”對象的生日,“小小小小羊”對象的生日將不會被修改,說明“小小小小羊”對象和“小羊”對象指向了不同的data對象。
序列化和反序列化實現深複製demo
深複製的實現不止上面一種,我們也可以利用序列化和反序列化來實現深複製,此時我們的Sheep類不僅要實現Cloneable接口,也要實現序列化接口Serializable接口。代碼如下:
public class Sheep implements Cloneable, Serializable {
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
}
public Sheep(String sname, Date birthday) {
this.sname = sname;
this.birthday = birthday;
}
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 class Client3 {
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Date date = new Date(123123123L);
Sheep sheep = new Sheep("小羊", date);
System.out.println("小羊對象爲:" + sheep);
System.out.println("小羊名字爲:" + sheep.getSname());
System.out.println("小羊生日爲:" + sheep.getBirthday());
//使用序列化和反序列化實現深複製
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(sheep);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
//克隆出“小小小小羊”
Sheep s2 = (Sheep) ois.readObject();
date.setTime(12345123L);
System.out.println("小羊生日修改後爲:" + sheep.getBirthday());
s2.setSname("小小小小羊");
System.out.println("克隆出的小小小小羊對象爲:" + s2);
System.out.println("克隆出的小小小小羊名字爲:" + s2.getSname());
System.out.println("克隆出的小小小小羊生日爲:" + s2.getBirthday());
}
}
結果如下:
可以看到,序列化和反序列化運行出的結果和上面我們深複製的運行結果相同,說明使用序列化和反序列化成功實現了深複製。
原型模式效率測試
通過new產生一個對象需要非常繁瑣的數據準備或者訪問權限,可以使用原型模式。那麼使用原型模式,和使用new來創建新對象相比,效率如何,我們用以下代碼來測試。
public class Computer implements Cloneable{
public Computer(){
try {
//模擬創建對象耗時的過程
Thread.sleep(10);
} catch (InterruptedException e){
e.printStackTrace();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
return obj;
}
}
public class Client4 {
public static void testNew(int size) {
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
Computer c = new Computer();
}
long end = System.currentTimeMillis();
System.out.println("new的方式創建耗時:" + (end - start) + "ms");
}
public static void testClone(int size) throws CloneNotSupportedException {
long start = System.currentTimeMillis();
Computer c = new Computer();
for (int i = 0; i < size; i++) {
Computer temp = (Computer) c.clone();
}
long end = System.currentTimeMillis();
System.out.println("克隆的方式創建耗時:" + (end - start) + "ms");
}
public static void main(String[] args) throws CloneNotSupportedException {
testNew(1000);
testClone(1000);
}
}
運行結果如下所示:
可以看到使用原型模式克隆對象比使用new來創建對象快了很多。但是在實際應用中,原型模式很少單獨出現,一般是和工廠方法模式一起出現,通過clone的方法創建一個對象,然後由工廠方法提供給調用者。spring中bean的創建就使用了單例模式和原型模式。
原型模式類圖
原型模式使用場景
Cloneable接口和Clone()
JavaScript中的繼承裏面的prototype
以上爲原型模式的學習筆記,此文章爲尚學堂視頻的學習筆記+自己總結。