디자인 모드11_원형 모드 (prototype) 딥 복사, 얕 은 복사

7779 단어 prototype
디자인 모드11_원형 모드 (prototype)
얕 은 복사:
package designPatternOf23;

/**

 *   :     ,         ,               

 * Prototype   :  Cloneable        。

 *   Object  clone  ,    protected    public

 *       :1、 object     ,      ,      ,      。

 *             2、    ,         

 *   :1、    ,          。

 *         2、     clone  ,   private  ,     

 *         3、   、   。   :

 *             clone       “      ”,    、    、          。

 *                           

 * */

// 01 :     

public class _05Prototype implements Cloneable {



    //  :clone  , Object  protect  public

    @Override

    public _05Prototype clone() throws CloneNotSupportedException {

        _05Prototype prototype=(_05Prototype)super.clone();

        return prototype;

    }

    

}

 
package designPatternOf23;



// 02 :    :   

public class _05ConcretePrototype_01 extends _05Prototype{

    public void show(){

        System.out.println("     show  ");

    }

}

 
package designPatternOf23;

// 03 :     :   

public class _05Client_01 {

    public static void main(String[] args) {

        _05ConcretePrototype_01 cp=new _05ConcretePrototype_01();

        for(int i=0;i<1000000;i++){

            try {

                _05ConcretePrototype_01 cloneCP=(_05ConcretePrototype_01)cp.clone();

                cloneCP.show();

            } catch (CloneNotSupportedException e) {

                e.printStackTrace();

            }

        }

    }

}

 
깊 은 복사:
package designPatternOf23;

/**

 *   :     ,         ,               

 * Prototype   :  Cloneable        。

 *   Object  clone  ,    protected    public

 *       :1、 object     ,      ,      ,      。

 *             2、    ,         

 *   :1、    ,          。

 *         2、     clone  ,   private  ,     

 *         3、   、   。   :

 *             clone       “      ”,    、    、          。

 *                           

 * */

// 01 :     

public class _05Prototype implements Cloneable {



    //  :clone  , Object  protect  public

    @Override

    public _05Prototype clone() throws CloneNotSupportedException {

        _05Prototype prototype=(_05Prototype)super.clone();

        return prototype;

    }

    

}

 
package designPatternOf23;



import java.util.ArrayList;



// 02 :    :   

public class _05ConcretePrototype_02 extends _05Prototype{

    private ArrayList arrayList=new ArrayList();

    _05ConcretePrototype_02 cp=null;

    @Override

    public _05Prototype clone() throws CloneNotSupportedException {

        cp=(_05ConcretePrototype_02) super.clone();

        cp.arrayList=(ArrayList) this.arrayList.clone();

        return cp;

    }



    public void show(){

        System.out.println("     show  ");

    }

}

 
package designPatternOf23;

// 03 :     :   

public class _05Client_02 {

    public static void main(String[] args) {

        _05ConcretePrototype_01 cp=new _05ConcretePrototype_01();

        for(int i=0;i<1000000;i++){

            try {

                _05ConcretePrototype_01 cloneCP=(_05ConcretePrototype_01)cp.clone();

                cloneCP.show();

            } catch (CloneNotSupportedException e) {

                e.printStackTrace();

            }

        }

    }

}

좋은 웹페이지 즐겨찾기