4_原型模式

2021-01-03  本文已影响0人  真是个点子王

传统方法

public class Client {
    public static void main(String[] args) {
        // 传统方法
        Sheep sheep = new Sheep("tom","白色",1);
        Sheep sheep1 = new Sheep(sheep.getName(),sheep.getColor(),sheep.getAge());
        Sheep sheep2 = new Sheep(sheep.getName(),sheep.getColor(),sheep.getAge());
        System.out.println(sheep);
        System.out.println(sheep1);
        System.out.println(sheep2);
    }
}

原型模式

利用Cloneable接口实现浅拷贝

// 这里需要实现Cloneable,并在后面重写clone方法
public class Sheep implements Cloneable{
    private String name;
    private String color;
    private int age;

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

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

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

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

    // 克隆该实例,使用默认的clone方法来完成
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

浅拷贝

public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        Sheep sheep = new Sheep("tom","白色",1,new Person("John"));
        Sheep sheep1 = (Sheep) sheep.clone();
        Sheep sheep2 = (Sheep) sheep.clone();
        System.out.println(sheep);
        System.out.println(sheep.getOnwer().hashCode());
        System.out.println(sheep1);
        System.out.println(sheep1.getOnwer().hashCode());
        System.out.println(sheep2);
        System.out.println(sheep2.getOnwer().hashCode());
        sheep.onwer.setName("Alice");
        System.out.println(sheep);
        System.out.println(sheep.getOnwer().hashCode());
        System.out.println(sheep1);
        System.out.println(sheep1.getOnwer().hashCode());
        System.out.println(sheep2);
        System.out.println(sheep2.getOnwer().hashCode());
    }
}

深拷贝

@Override
    protected Object clone() throws CloneNotSupportedException {
        DeepProtoType deep = null;

        // 这里完成对基本数据类型和String的克隆
        deep = (DeepProtoType)super.clone();
        // 对引用类型的数据,进行单独处理
        deep.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
        return deep;
    }

// 方式2 通过对象序列化实现
    public Object deepClone(){
        // 创建流对象
        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);
            DeepProtoType deepProtoType = (DeepProtoType) ois.readObject();
            return deepProtoType;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

上一篇 下一篇

猜你喜欢

热点阅读