设计模式-原型模式实现方式-深拷贝重写colne方法或者利用序列化


/**
* 实现Cloneable的接口,使其具有clone的能力
*/
public class Product implements  Cloneable{
   private String name;
   private String part1;
   private String part2;
   private int age;
   private ProductBase productBase;

   public String getName() {
       return name;
   }

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

   public String getPart1() {
       return part1;
   }

   public void setPart1(String part1) {
       this.part1 = part1;
   }

   public String getPart2() {
       return part2;
   }

   public void setPart2(String part2) {
       this.part2 = part2;
   }

   public int getAge() {
       return age;
   }

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

   public ProductBase getProductBase() {
       return productBase;
   }

   public void setProductBase(ProductBase productBase) {
       this.productBase = productBase;
   }

   public Product(){}

   public Product(String name,String part1,String part2,int age,ProductBase productBase){
       this.name=name;
       this.part1=part1;
       this.part2=part2;
       this.age=age;
       this.productBase=productBase;
   }

   public Product(String name,String part1,String part2,int age){
       this.name=name;
       this.part1=part1;
       this.part2=part2;
       this.age=age;
   }

   //将colne后的数据直接转为Product
   @Override
   protected Product clone() throws CloneNotSupportedException {
       //没有引用类型
       //return ((Product)super.clone());
       //需要clone的对象带引用类型,
       Product clone=(Product)super.clone();
       ProductBase productBaseClone=this.productBase.clone();
       clone.setProductBase(productBaseClone);
       return clone;
   }


   @Override
   public String toString() {
       return super.hashCode()+"=Product{" +
               "name='" + name + '\'' +
               ", part1='" + part1 + '\'' +
               ", part2='" + part2 + '\'' +
               ", age=" + age +
               ", productBase=" + productBase +
               '}';
   }
}

深度克隆,也叫深拷贝,有两种实现方式:
1、重写clone方法


public class ProductBase implements Cloneable{
    private int productId;

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    @Override
    protected ProductBase clone() throws CloneNotSupportedException {
        return (ProductBase)super.clone();
    }
}


public class PrototypeTest {
    public static void main(String[] args) throws CloneNotSupportedException {
//        Product product=new Product("sunyuhua","part1","part2",2);
//        Product product2=product.clone();

//        System.out.println("克隆的两个对象是否相等:"+(product==product2));
//        System.out.println("克隆的两个对象是否相等:"+product.equals(product2));
//        System.out.println("克隆的两个对象的值是否相等:"+product.getName().equals(product2.getName()));

        ProductBase productBase=new ProductBase();
        productBase.setProductId(1);
        Product product3=new Product("sunyuhua3","part1","part2",2,productBase);
        Product clone = product3.clone();
        System.out.println(product3);
        System.out.println(clone);
        System.out.println("被clone的引用对象的里面的值==="+clone.getProductBase().getProductId());
        clone.getProductBase().setProductId(3);
        System.out.println("被clone的引用对象的里面的值"+clone.getProductBase().getProductId());


    }
}

第二:通过序列化和反序列化机制


public class DeepCloneTarget implements Serializable,Cloneable {

    private  String cloneName;
    private String cloneClass;
    public DeepCloneTarget(String cloneName,String cloneClass){
        this.cloneName=cloneName;
        this.cloneClass=cloneClass;
    }

    public String getCloneName() {
        return cloneName;
    }

    public void setCloneName(String cloneName) {
        this.cloneName = cloneName;
    }

    public String getCloneClass() {
        return cloneClass;
    }

    public void setCloneClass(String cloneClass) {
        this.cloneClass = cloneClass;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


public class DeepProtoType  implements Serializable,Cloneable {
    public String name;
    public DeepCloneTarget deepCloneTarget;
    public DeepProtoType(){
        super();
    }



    /**
     * 深拷贝-方式1 使用clone()方法来做
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object deep=null;
        //这里对基本类型属性和string进行了克隆
        deep=super.clone();
        //对引用类型的属性,进行了单独处理
        DeepProtoType deepProtoType=(DeepProtoType)deep;//也就是处理了name
        deepProtoType.deepCloneTarget=deepCloneTarget;
        return deepProtoType;
    }

    /**
     * 深拷贝的另一种方法,通过序列化来实现
     * @return
     */
    protected Object deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos=null;
        ObjectOutputStream oos=null;
        ByteArrayInputStream bis=null;
        ObjectInputStream ois=null;
        //序列化
        bos=new  ByteArrayOutputStream();
        oos=new ObjectOutputStream(bos);
        //将当前对象以流的方式输出
        oos.writeObject(this);
        //反序列化
        bis=new ByteArrayInputStream(bos.toByteArray());
        ois=new ObjectInputStream(bis);
        Object object=ois.readObject();
        return object;
    }

}

public class DeeepCloneClient {
    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {

        DeepProtoType deepProtoType=new DeepProtoType();
        deepProtoType.name="ssdasdasd";
        deepProtoType.deepCloneTarget=new DeepCloneTarget("sunyuhua","NBclass");
        DeepProtoType deepProtoType1= (DeepProtoType) deepProtoType.clone();
        DeepProtoType deepProtoType2= (DeepProtoType) deepProtoType.clone();
        System.out.println(deepProtoType2.name);
        System.out.println(deepProtoType2.deepCloneTarget.getCloneClass());
        System.out.println(deepProtoType2.deepCloneTarget.getCloneName());
        System.out.println(deepProtoType1==deepProtoType2);
        System.out.println(deepProtoType1.hashCode());
        System.out.println(deepProtoType2.hashCode());
        System.out.println(deepProtoType1.equals(deepProtoType2));
        System.out.println("++++++++++++++++++++++++++++++++");

        DeepProtoType deepProtoType3=new DeepProtoType();
        deepProtoType3.name="sunyuhua";
        deepProtoType3.deepCloneTarget=new DeepCloneTarget("zhangsan","zhansanclassName");
        DeepProtoType deepProtoType4 = (DeepProtoType)deepProtoType3.deepClone();
        System.out.println(deepProtoType4==deepProtoType3);
        System.out.println(deepProtoType4.hashCode());
        System.out.println(deepProtoType3.hashCode());
        System.out.println(deepProtoType4.name);
        System.out.println(deepProtoType4.deepCloneTarget.getCloneName());
        System.out.println(deepProtoType4.deepCloneTarget.getCloneClass());
        System.out.println(deepProtoType3.equals(deepProtoType4));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值