原型模式(深拷贝)

public class DeepCloneSheep implements Cloneable, Serializable {
    String name;
    String address;
    DeepSheepFriend deepSheepFriend;

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

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

    public DeepSheepFriend getDeepSheepFriend() {
        return deepSheepFriend;
    }

    public DeepCloneSheep(String name, String address, DeepSheepFriend deepSheepFriend) {
        this.name = name;
        this.address = address;
        this.deepSheepFriend = deepSheepFriend;
    }

    public void setDeepSheepFriend(DeepSheepFriend deepSheepFriend) {
        this.deepSheepFriend = deepSheepFriend;
    }

//重写clone方法实现深复制
    @Override
    protected Object clone() throws CloneNotSupportedException {
        DeepCloneSheep deepCloneSheep = null;
     //要注意的是,因为DeepCloneSheep的成员变量是String类型,不是数组或者对象等,可以调用clone方法
        DeepCloneSheep deepCloneSheepclone = ( DeepCloneSheep ) super.clone ();
        DeepSheepFriend deepSheepFriend = deepCloneSheepclone.getDeepSheepFriend ();
        DeepSheepFriend deepSheepFriendclone = ( DeepSheepFriend ) deepSheepFriend.clone ();
        deepCloneSheepclone.setDeepSheepFriend (deepSheepFriendclone);
        return deepCloneSheepclone;
    }
//利用序列化实现深复制
    protected Object serializableClone() {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            bos = new ByteArrayOutputStream ();
            oos = new ObjectOutputStream (bos);
            oos.writeObject (this);
            bis = new ByteArrayInputStream (bos.toByteArray ());
            ois = new ObjectInputStream (bis);
            DeepCloneSheep deepCloneSheep = ( DeepCloneSheep ) ois.readObject ();
            return deepCloneSheep;
        } catch (ClassNotFoundException e) {
            e.printStackTrace ();
            return null;
        } catch (IOException e) {
            e.printStackTrace ();
            return null;
        } finally {
            try {
                bos.close ();
                oos.close ();
                bis.close ();
                ois.close ();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        DeepSheepFriend deepSheepFriend = new DeepSheepFriend ("sheep的朋友");
        DeepCloneSheep deepCloneSheep = new DeepCloneSheep ("sheep", "beijing", deepSheepFriend);
        DeepCloneSheep clone = ( DeepCloneSheep ) deepCloneSheep.clone ();
        System.out.println ("重写clone方法深拷贝");
        System.out.println (clone == deepCloneSheep);
        System.out.println (clone.getDeepSheepFriend () == deepCloneSheep.getDeepSheepFriend ());
        DeepCloneSheep deepCloneSheep1 =(DeepCloneSheep) deepCloneSheep.serializableClone ();
        System.out.println ("序列化深拷贝(建议使用)");
        System.out.println (clone == deepCloneSheep1);
        System.out.println (clone.getDeepSheepFriend () == deepCloneSheep1.getDeepSheepFriend ());
    }
}

class DeepSheepFriend implements Cloneable, Serializable {
    String friendName;

    public String getFriendName() {
        return friendName;
    }

    public void setFriendName(String friendName) {
        this.friendName = friendName;
    }

    public DeepSheepFriend(String friendName) {
        this.friendName = friendName;
    }

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

深拷贝总结:
1.复制对象的所有基本数据类型的成员变量
2.为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象,也就是说,对象进行深拷贝要对整个对象(包括对象的引用类型)进行拷贝.

上面的代码中展示了两种方法积极性深拷贝,重写clone方法和序列化,clone需要每个引用变量单独调用clone方法,如果引用成员变量多的话,重复性代码多,不便于维护,这里推荐是使用序列化方式深拷贝.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。