设计模式 原型模式(笔记)

//只用作给Sheep充当引用对象
public class Country implements Cloneable, Serializable {
    //序列化的版本号
    private static final long serialVersionUID = 1001L;
    private String mainland;

    public Country(String mainland) {
        this.mainland = mainland;
    }

    public String getMainland() {
        return mainland;
    }

    public void setMainland(String mainland) {
        this.mainland = mainland;
    }

    @Override
    public String toString() {
        return "Country{" +
                "mainland='" + mainland + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Sheep implements Cloneable, Serializable {
    //序列化的版本号
    private static final long serialVersionUID = 1000L;
    private String name;
    private int age;
    private String color;
    private Country country;

    public Sheep(String name, int age, String color, Country country) {
        this.name = name;
        this.age = age;
        this.color = color;
        this.country = country;
        System.out.println("走构造器创建对象");
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    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;
    }

    public String getColor() {
        return color;
    }

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

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

    //需要实现Cloneable接口
    //实现深克隆方式一
    @Override
    protected Sheep clone() throws CloneNotSupportedException {
        //对基本数据进行复制,对除string外的引用对象进行地址拷贝
        Object clone = super.clone();
        Sheep sheep = (Sheep) clone;
        //调用属性中的引用类型实现的clone()方法,达到深拷贝
        sheep.country = (Country) country.clone();
        return sheep;
    }

    //需要该类和属性中的类实现 Serializable接口
    //实现深克隆方式二
    public Sheep deepClone() {
        //创建流对象
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        ByteArrayInputStream bis = null;
        ByteArrayOutputStream bos = null;
        Sheep sheep = null;
        try {
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);


            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            sheep = (Sheep) ois.readObject();

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //关闭所有流
            try {
                if (ois == null) {
                    ois.close();
                }
                if (bis == null) {
                    bis.close();
                }
                if (oos == null) {
                    oos.close();
                }
                if (bos == null) {
                    bos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
       return sheep;
    }
}
public class TestClone {
    public static void main(String[] args) throws Exception{
        //走构造克隆对象
        Sheep sheep = new Sheep("阿伟", 5, "黄色",new Country("亚洲"));
        //走克隆对象
        Sheep cloneSheep = sheep.deepClone();
         /*对比原型与克隆对象的地址(可以深克隆与浅克隆)
        * 删除方法中 sheep.country = (Country) country.clone();是浅克隆,地址一样
        *  sheep.country = (Country) country.clone();是深克隆,地址不一样
        * */
        System.out.println("原型羊的地址"+sheep.hashCode());
        System.out.println("克隆羊的地址"+cloneSheep.hashCode());
        //对比引用对象country的地址(走的深克隆)
        System.out.println("原型羊的国家地址"+sheep.getCountry().hashCode());
        System.out.println("克隆羊的国家地址"+cloneSheep.getCountry().hashCode());
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值