原型模式
– 通過new產生一個對象需要非常繁瑣的數據準備或訪問權限,則可以使用原型模式。
– 就是java中的克隆技術,以某個對象爲原型,複製出新的對象。顯然,新的對象具備原型對象的特點
– 優勢有:效率高(直接克隆,避免了重新執行構造過程步驟) 。
– 克隆類似於new,但是不同於new。new創建新的對象屬性採用的是默認值。克隆出的對象的屬性值完全和原型對象相同。並且克隆出的新對象改變不會影響原型對象。然後,
再修改克隆對象的值。
開發中的應用場景
– 原型模式很少單獨出現,一般是和工廠方法模式一起出現,通過clone的方法創建一個對象,然後由工廠方法提供給調用者。
• spring中bean的創建實際就是兩種:單例模式和原型模式。(當然,原型模式需要和工廠模式搭配起來)
原型模式實現:
– Cloneable接口和clone方法
– Prototype模式中實現起來最困難的地方就是內存複製操作,所幸在Java中提供了clone()方法替我們做了絕大部分事情。
原型模式案例:
//原型模式:潛複製
public class Sheep implements Cloneable,Serializable { //1997,英國的克隆羊,多利!
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone(); //直接調用object對象的clone()方法!
return obj;
}
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 Sheep(String sname, Date birthday) {
super();
this.sname = sname;
this.birthday = birthday;
}
public Sheep() {
}
}
/**
* 測試原型模式(淺克隆)
*/
public class Client {
public static void main(String[] args) throws Exception {
Date date = new Date(12312321331L);
Sheep s1 = new Sheep("少利",date);
System.out.println(s1);
System.out.println(s1.getSname());
System.out.println(s1.getBirthday());
date.setTime(23432432423L);
System.out.println(s1.getBirthday());
Sheep s2 = (Sheep) s1.clone();
s2.setSname("多利");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
淺克隆存在的問題:
– 被複制的對象的所有變量都含有與原來的對象相同的值,而所有的對其他對象的引用都仍然指向原來的對象。
深克隆如何實現
– 深克隆把引用的變量指向複製過的新對象,而不是原有的被引用的對象。
– 深克隆:讓已實現Clonable接口的類中的屬性也實現Clonable接口
– 基本數據類型和String能夠自動實現深度克隆(值的複製)
深克隆實現(實現Cloneable 接口)
//測試深複製
public class Sheep2 implements Cloneable { //1997,英國的克隆羊,多利!
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone(); //直接調用object對象的clone()方法!
//添加如下代碼實現深複製(deep Clone)
Sheep2 s = (Sheep2) obj;
s.birthday = (Date) this.birthday.clone(); //把屬性也進行克隆!
return obj;
}
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 Sheep2(String sname, Date birthday) {
super();
this.sname = sname;
this.birthday = birthday;
}
public Sheep2() {
}
}
/**
* 原型模式(深複製)
*/
public class Client2 {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(12312321331L);
Sheep2 s1 = new Sheep2("少利",date);
Sheep2 s2 = (Sheep2) s1.clone(); //實現深複製。s2對象的birthday是一個新對象!
System.out.println(s1);
System.out.println(s1.getSname());
System.out.println(s1.getBirthday());
date.setTime(23432432423L);
System.out.println(s1.getBirthday());
s2.setSname("多利");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
深克隆實現(使用序列化和反序列化的方式實現)
/**
* 原型模式(深複製,使用序列化和反序列化的方式實現深複製)
*/
public class Client3 {
public static void main(String[] args) throws CloneNotSupportedException, Exception {
Date date = new Date(12312321331L);
Sheep s1 = new Sheep("少利",date);
System.out.println(s1);
System.out.println(s1.getSname());
System.out.println(s1.getBirthday());
// 使用序列化和反序列化實現深複製
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(s1);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
Sheep s2 = (Sheep) ois.readObject(); //克隆好的對象!
System.out.println("修改原型對象的屬性值");
date.setTime(23432432423L);
System.out.println(s1.getBirthday());
s2.setSname("多利");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
短時間大量創建對象時,原型模式和普通new方式效率測試
/**
* 測試普通new方式創建對象和clone方式創建對象的效率差異!
* 如果需要短時間創建大量對象,並且new的過程比較耗時。則可以考慮使用原型模式!
*/
public class Client4 {
public static void testNew(int size){
long start = System.currentTimeMillis();
for(int i=0;i<size;i++){
Laptop t = 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;i++){
Laptop temp = (Laptop) t.clone();
}
long end = System.currentTimeMillis();
System.out.println("clone的方式創建耗時:"+(end-start));
}
public static void main(String[] args) throws Exception {
testNew(1000);
testClone(1000);
}
}
class Laptop implements Cloneable { //筆記本電腦
public Laptop() {
try {
Thread.sleep(10); //模擬創建對象耗時的過程!
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone(); //直接調用object對象的clone()方法!
return obj;
}
}