国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

設計模式-原型模式

2019-11-11 06:36:55
字體:
來源:轉載
供稿:網友

1.原型模式的定義及使用場景

定義:用原型實例指定創建對象的種類,并通過拷貝這些原型創建新的對象使用場景:1)類初始化需要消耗非常多的資源,這個資源包括數據、硬件資源等,通過原型拷貝避免這些消耗2)通過new產生一個對象需要非常繁瑣的數據準備或訪問權限,這時可以使用原型模式

3)一個對象需要提供給其他對象訪問,而且各個調用者可能都需要修改其值時,可以考慮使用原型模式拷貝多個對象供調用者使用,即保護性拷貝

2.原型模式的優缺點

2.1優點

性能優良原型模式是在內存二進制流的拷貝,要比直接new一個對象性能好,特別是要在一個循環體內產生大量的對象時,原型模式可以更好地體現其優點

2.2缺點

逃避構造函數的約束這既是他的優點也是缺點,直接在內存中拷貝,構造函數不會執行。需要在實際應用時考慮

3.注意實現

1)構造函數默認不執行2)淺拷貝及深拷貝Object類提供的方法clone只是拷貝本對象,其對象內部的數組、引用對象等都不拷貝,還是指向原型對象的內部元素地址,這種拷貝為淺拷貝。如需要深拷貝,對應的成員也需指向clone方法3)要使用clone方法,類的成員變量上不要增加final關鍵字

4.原型模式的實現方式

PRotoType:public class ProtoType implements Cloneable {    public ProtoType() {        System.out.println("ProtoType is excute...");    }    private int id;    private String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    protected ProtoType clone() {        ProtoType protoType = null;        try {            protoType = (ProtoType) super.clone();        } catch (CloneNotSupportedException e) {            e.printStackTrace();        }        return protoType;    }    @Override    public String toString() {        return "ProtoType{" +                "id=" + id +                ", name='" + name + '/'' +                '}';    }}Text:public class Test {    public static void main(String args[]) {        ProtoType type = new ProtoType();        type.setId(1);        type.setName("張三");        System.out.println(type);        ProtoType clone = type.clone();        clone.setId(2);        clone.setName("李四");        System.out.println(clone);    }}
Objec的clone源碼:  /**  * Creates and returns a copy of this {@code Object}. The default  * implementation returns a so-called "shallow" copy: It creates a new  * instance of the same class and then copies the field values (including  * object references) from this instance to the new instance. A "deep" copy,  * in contrast, would also recursively clone nested objects. A subclass that  * needs to implement this kind of cloning should call {@code super.clone()}  * to create the new instance and then create deep copies of the nested,  * mutable objects.  *  * @return a copy of this object.  * @throws CloneNotSupportedException  * if this object's class does not implement the {@code  * Cloneable} interface.  */  protected Object clone() throws CloneNotSupportedException {  if (!(this instanceof Cloneable)) {  throw new CloneNotSupportedException("Class " + getClass().getName() +  " doesn't implement Cloneable");  }  return internalClone();  }  /*  * Native helper method for cloning.  */  private native Object internalClone();可見執行了一個native方法執行二進制流的拷貝

5.原型模式在Android中的實際應用

Intent:  @Override  public Object clone() {  return new Intent(this);  } /**  * Copy constructor.  */  public Intent(Intent o) {  this.mAction = o.mAction;  this.mData = o.mData;  this.mType = o.mType;  this.mPackage = o.mPackage;  this.mComponent = o.mComponent;  this.mFlags = o.mFlags;  this.mContentUserHint = o.mContentUserHint;  if (o.mCategories != null) {  this.mCategories = new ArraySet<String>(o.mCategories);  }  if (o.mExtras != null) {  this.mExtras = new Bundle(o.mExtras);  }  if (o.mSourceBounds != null) {  this.mSourceBounds = new Rect(o.mSourceBounds);  }  if (o.mSelector != null) {  this.mSelector = new Intent(o.mSelector);  }  if (o.mClipData != null) {  this.mClipData = new ClipData(o.mClipData);  }  }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 杨浦区| 九龙城区| 崇阳县| 疏勒县| 分宜县| 通渭县| 宁陕县| 甘孜县| 乌兰浩特市| 青阳县| 商城县| 绥江县| 赤峰市| 望奎县| 正阳县| 海丰县| 江北区| 庆云县| 茶陵县| 基隆市| 凤冈县| 栖霞市| 蒙城县| 科技| 岳阳县| 雅江县| 刚察县| 辽源市| 湘潭市| 随州市| 沁阳市| 华阴市| 瑞丽市| 吉隆县| 扎囊县| 化隆| 土默特右旗| 镇雄县| 临清市| 嘉黎县| 巨鹿县|