設(shè)計模式系列--原型模式

定義

原型模式屬于對象的創(chuàng)建模式。通過給出一個原型對象來指明所有創(chuàng)建的對象的類型,然后用復制這個原型對象的辦法創(chuàng)建出更多同類型的對象。這就是原型模式的用意。

原型模式的結(jié)構(gòu)

原型模式要求對象實現(xiàn)一個可以“克隆”自身的接口,這樣就可以通過復制一個實例對象本身來創(chuàng)建一個新的實例。這樣一來,通過原型實例創(chuàng)建新的對象,就不再需要關(guān)心這個實例本身的類型,只要實現(xiàn)了克隆自身的方法,就可以通過這個方法來獲取新的對象,而無須再去通過new來創(chuàng)建。

定必比較抽象,下面通過一個例子,來介紹原型模式;
首先我們定義一個Person類

public class Person{
    private String name;
    private int age;
    private double height;
    private double weight;

    public Person(){
        
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                ", weight=" + weight +
                '}';
    }
}

要實現(xiàn)原型模式,只需要按照下面的幾個步驟去實現(xiàn)即可。

  • 實現(xiàn)Cloneable接口
public class Person implements Cloneable{

}
  • 重寫Object的clone方法
@Override
public Object clone(){
    return null;
}
  • 實現(xiàn)clone方法中的拷貝邏輯
@Override
public Object clone(){
    Person person=null;
    try {
        person=(Person)super.clone();
        person.name=this.name;
        person.weight=this.weight;
        person.height=this.height;
        person.age=this.age;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    return person;
}

測試一下

public class Main {
    public static void main(String [] args){
        Person p=new Person();
        p.setAge(18);
        p.setName("張三");
        p.setHeight(178);
        p.setWeight(65);
        System.out.println(p);

        Person p1= (Person) p.clone();
        System.out.println(p1);

        p1.setName("李四");
        System.out.println(p);
        System.out.println(p1);
    }
}

輸出結(jié)果如下:

Person{name=’張三’, age=18, height=178.0, weight=65.0}
Person{name=’張三’, age=18, height=178.0, weight=65.0}
Person{name=’張三’, age=18, height=178.0, weight=65.0}
Person{name=’李四’, age=18, height=178.0, weight=65.0}

試想一下,兩個不同的人,除了姓名不一樣,其他三個屬性都一樣,用原型模式進行拷貝就會顯得異常簡單,這也是原型模式的應用場景之一。

一個對象需要提供給其他對象訪問,而且各個調(diào)用者可能都需要修改其值時,可以考慮使用原型模式拷貝多個對象供調(diào)用者使用,即保護性拷貝。
但是假設(shè)Person類里還有一個屬性叫興趣愛好,是一個List集合,就像這樣子

private ArrayList<String> hobbies=new ArrayList<String>();

public ArrayList<String> getHobbies() {
    return hobbies;
}

public void setHobbies(ArrayList<String> hobbies) {
    this.hobbies = hobbies;
}

在進行拷貝的時候要格外注意,如果你直接按之前的代碼那樣拷貝

@Override
public Object clone(){
    Person person=null;
    try {
        person=(Person)super.clone();
        person.name=this.name;
        person.weight=this.weight;
        person.height=this.height;
        person.age=this.age;
//person.hobbies=this.hobbies 這種方式只進行了淺拷貝,也就是只拷貝了引用,最終兩個對象指向的引用是同一個,一個發(fā)生變化另一個也會發(fā)生變換,顯然解決方法就是使用深拷貝。
        //person.hobbies=this.hobbies;
person.hobbies=(ArrayList<String>)this.hobbies.clone();

    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    return person;
}

其實有時候我們會更多的看到原型模式的另一種寫法。

  • 在clone函數(shù)里調(diào)用構(gòu)造函數(shù),構(gòu)造函數(shù)的入?yún)⑹窃擃悓ο蟆?/li>
@Override
public Object clone(){
    return new Person(this);
}
  • 在構(gòu)造函數(shù)中完成拷貝邏輯
public Person(Person person){
    this.name=person.name;
    this.weight=person.weight;
    this.height=person.height;
    this.age=person.age;
    this.hobbies= new ArrayList<String>(hobbies);
}

其實都差不多,只是寫法不一樣。
現(xiàn)在來挖挖android中的原型模式。
先看Bundle類,該類實現(xiàn)了Cloneable接口

public Object clone() {
    return new Bundle(this);
} 
public Bundle(Bundle b) {
    super(b);

    mHasFds = b.mHasFds;
    mFdsKnown = b.mFdsKnown;
}

然后是Intent類,該類也實現(xiàn)了Cloneable接口

@Override
public Object clone() {
    return new Intent(this);
}
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);
    }
}

用法也顯得十分簡單,一旦我們要用的Intent與現(xiàn)有的一個Intent很多東西都是一樣的,那我們就可以直接拷貝現(xiàn)有的Intent,再修改不同的地方,便可以直接使用。

Uri uri = Uri.parse("smsto:10086");    
Intent shareIntent = new Intent(Intent.ACTION_SENDTO, uri);    
shareIntent.putExtra("sms_body", "hello");    

Intent intent = (Intent)shareIntent.clone() ;
startActivity(intent);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容