原型模式案例(代码)讲解

设计模式的代码:https://github.com/Aerozb/design_patterns

浅拷贝

public class Sheep implements Cloneable {

    private String name;
    // 基本数据类型能自动实现深度clone。
    private int age;
    //浅拷贝,每个clone对象都是引用同一个对象
    public Sheep sheep;

    public Sheep getSheep() {
        return sheep;
    }

    public void setSheep(Sheep sheep) {
        this.sheep = sheep;
    }

    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sheep=" + sheep +
                '}';
    }

    @Override
    protected Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

}
public class Client {
    public static void main(String[] args) {
        Sheep sheep = new Sheep("aa", 12);
        sheep.sheep= new Sheep("bb", 11);
        Sheep clone1 = (Sheep) sheep.clone();
        Sheep clone2 = (Sheep)sheep.clone();
        System.out.println("clone1的String类型name:"+clone1.getName().hashCode());
        System.out.println("clone2的String类型name:"+clone2.getName().hashCode());
        System.out.println("clone1的Sheep类型:"+clone1.getSheep().hashCode());
        System.out.println("clone2的Sheep类型:"+clone2.getSheep().hashCode());
    }
}

结果

clone1的String类型name:3104
clone2的String类型name:3104
clone1的Sheep类型:460141958
clone2的Sheep类型:460141958

深拷贝

//使用序列化方式进行拷贝,因为序列化会进行深拷贝
public class Sheep implements Serializable {

    //String克隆都是一样的
    private String name;
    // 基本数据类型能自动实现深度clone。
    private int age;
    //浅拷贝,每个clone对象都是引用同一个对象
    public Sheep sheep;

    public Sheep getSheep() {
        return sheep;
    }

    public void setSheep(Sheep sheep) {
        this.sheep = sheep;
    }

    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sheep=" + sheep +
                '}';
    }

    public Object deepClone() {
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;

        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            Sheep sheep = (Sheep) ois.readObject();
            return sheep;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                bis.close();
                ois.close();
                bos.close();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Client {
    public static void main(String[] args) {
        Sheep sheep = new Sheep("aa", 12);
        sheep.sheep= new Sheep("bb", 11);
        Sheep clone1 = (Sheep) sheep.deepClone();
        Sheep clone2 = (Sheep)sheep.deepClone();
        System.out.println("clone1的String类型name:"+clone1.getName().hashCode());
        System.out.println("clone2的String类型name:"+clone2.getName().hashCode());
        System.out.println("clone1的Sheep类型:"+clone1.getSheep().hashCode());
        System.out.println("clone2的Sheep类型:"+clone2.getSheep().hashCode());
    }
}

结果

clone1的String类型name:3104
clone2的String类型name:3104
clone1的Sheep类型:2129789493
clone2的Sheep类型:668386784

 

你可能感兴趣的:(JAVA,设计模式)