1. 程式人生 > 實用技巧 >設計模式 之 <一> 原型模式(Prototype Pattern)

設計模式 之 <一> 原型模式(Prototype Pattern)

<淺克隆>

public class Address {
    public Address(){}
    public Address(String city,String area){
        this.city=city;
        this.area=area;
    };
    //城市
    private String city;
    //區域
    private String area;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        
this.city = city; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } @NonNull @Override public String toString() { return city+","+area; } }

//客戶類
public class Customer implements  Cloneable{
    
//客戶地址 private Address address; //名稱 private String name; //電話 private String phone; //電話 private String businessType; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public
String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getBusinessType() { return businessType; } public void setBusinessType(String businessType) { this.businessType = businessType; } @Override protected Customer clone(){ try { return (Customer)super.clone(); } catch (CloneNotSupportedException e) { return null; } } }

  public static void main(){
      Customer customer_huawei=new Customer();
      customer_huawei.setName("華為");
      customer_huawei.setPhone("18888888888");
      customer_huawei.setBusinessType("手機");
      customer_huawei.setAddress(new Address("深圳","南山"));

      Customer customer_xiaomi =  customer_huawei.clone();
      customer_xiaomi.setName("小米");
      customer_xiaomi.setPhone("18666666666");

      Log.d(TAG,"customer_huawei==customer2 ? "+(customer_huawei==customer_xiaomi));
      Log.d(TAG,"customer_huawei.getAddress()==customer2.getAddress() ? "+(customer_huawei.getAddress()==customer_xiaomi.getAddress()));
      Log.d(TAG,"---------------------------");
      Log.d(TAG,"customer_huawei.name="+customer_huawei.getName());
      Log.d(TAG,"customer_huawei.phone="+customer_huawei.getPhone());
      Log.d(TAG,"customer_huawei.businessType="+customer_huawei.getBusinessType());
      Log.d(TAG,"customer_huawei.address="+customer_huawei.getAddress().toString());
      Log.d(TAG,"---------------------------");
      Log.d(TAG,"customer_xiaomi.name="+customer_xiaomi.getName());
      Log.d(TAG,"customer_xiaomi.phone="+customer_xiaomi.getPhone());
      Log.d(TAG,"customer_xiaomi.businessType="+customer_xiaomi.getBusinessType());
      Log.d(TAG,"customer_xiaomi.address="+customer_xiaomi.getAddress().toString());
      Log.d(TAG,"==============================");
  }




2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_huawei==customer2 ? false
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_huawei.getAddress()==customer2.getAddress() ? true
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: ---------------------------
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_huawei.name=華為
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_huawei.phone=18888888888
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_huawei.businessType=手機
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_huawei.address=深圳,南山
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: ---------------------------
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.name=小米
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.phone=18666666666
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.businessType=手機
2020-07-20 20:11:23.318 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.address=深圳,南山

<深度克隆>

public class Address implements Serializable {
    public Address(){}
    public Address(String city,String area){
        this.city=city;
        this.area=area;
    };
    //城市
    private String city;
    //區域
    private String area;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    @NonNull
    @Override
    public String toString() {
        return city+","+area;
    }
}
public class Customer2 implements Serializable {
    //客戶地址
    private Address address;
    //名稱
    private String name;
    //電話
    private String phone;
    //電話
    private String businessType;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getBusinessType() {
        return businessType;
    }

    public void setBusinessType(String businessType) {
        this.businessType = businessType;
    }

    @Override
    protected Customer2 clone(){
        try {
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);

            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
            Object object= objectInputStream.readObject();
            return (Customer2)object;
        } catch (Exception e) {
            Log.e("PrototypePattern",e.toString());
        }
        return  null;
    }
}
//深度克隆
public static void main2(){
Customer2 customer_huawei=new Customer2();
customer_huawei.setName("華為");
customer_huawei.setPhone("18888888888");
customer_huawei.setBusinessType("手機");
customer_huawei.setAddress(new Address("深圳","南山"));

Customer2 customer_xiaomi = customer_huawei.clone();
customer_xiaomi.setName("小米");
customer_xiaomi.setPhone("18666666666");

Log.d(TAG,"customer_huawei==customer2 ? "+(customer_huawei==customer_xiaomi));
Log.d(TAG,"customer_huawei.getAddress()==customer2.getAddress() ? "+(customer_huawei.getAddress()==customer_xiaomi.getAddress()));
Log.d(TAG,"---------------------------");
Log.d(TAG,"customer_huawei.name="+customer_huawei.getName());
Log.d(TAG,"customer_huawei.phone="+customer_huawei.getPhone());
Log.d(TAG,"customer_huawei.businessType="+customer_huawei.getBusinessType());
Log.d(TAG,"customer_huawei.address="+customer_huawei.getAddress().toString());
Log.d(TAG,"---------------------------");
Log.d(TAG,"customer_xiaomi.name="+customer_xiaomi.getName());
Log.d(TAG,"customer_xiaomi.phone="+customer_xiaomi.getPhone());
Log.d(TAG,"customer_xiaomi.businessType="+customer_xiaomi.getBusinessType());
Log.d(TAG,"customer_xiaomi.address="+customer_xiaomi.getAddress().toString());
}
 

2020-07-20 20:11:23.324 24649-24649/com.example.test D/PrototypePattern: customer_huawei==customer2 ? false 2020-07-20 20:11:23.324 24649-24649/com.example.test D/PrototypePattern: customer_huawei.getAddress()==customer2.getAddress() ? false 2020-07-20 20:11:23.324 24649-24649/com.example.test D/PrototypePattern: --------------------------- 2020-07-20 20:11:23.324 24649-24649/com.example.test D/PrototypePattern: customer_huawei.name=華為 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: customer_huawei.phone=18888888888 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: customer_huawei.businessType=手機 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: customer_huawei.address=深圳,南山 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: --------------------------- 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.name=小米 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.phone=18666666666 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.businessType=手機 2020-07-20 20:11:23.325 24649-24649/com.example.test D/PrototypePattern: customer_xiaomi.address=深圳,南山