首页 > 其他 > 详细

设计模式 之 <一> 原型模式(Prototype Pattern)

时间:2020-07-20 22:07:54      阅读:73      评论:0      收藏:0      [点我收藏+]

<浅克隆>

第一步:需要实现 Cloneable 接口

第二步:调用Object类的clone函数实现对象克隆。

备注:浅克隆只能克隆基本数据类型,引用类型还是原对象的引用实例。比如下面代码的Address 克隆之后还是,原对象的Address实例,没有生产新的Address实例。要解决这个问题需要实现深度克隆。


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=深圳,南山

 

<深度克隆>

第一步:实现Serializable 接口

第二步:通过序列号对象实现克隆。

备注:可以解决浅克隆,无法生成新的引用对象的问题。

 

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=深圳,南山

 

设计模式 之 <一> 原型模式(Prototype Pattern)

原文:https://www.cnblogs.com/jtzp007/p/13347450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!