设计模式-原型模式

- 原型模式

1.Cloneable接口、Object#clone方法详解

2.浅拷贝/深拷贝

3.序列化机制实现深拷贝

 

import java.io.*;

/**
 * 原型模式
 */
public class protoType {
    public static void main(String[] args) throws CloneNotSupportedException {
        BaseInfo baseInfo = new BaseInfo("companyA");
        Product product = new Product("part1", "part2", 3, baseInfo);
        Product clone = product.clone();
        System.out.println("original" + product);
        System.out.println("clone" + clone);

        product.getBaseInfo().setCompanyName("yyyy");
        System.out.println("original" + product);
        System.out.println("clone" + clone);
    }

}

class Product implements Cloneable, Serializable {
    private static final long serialVersionUID = 42L;
    private String part1;
    private String part2;
    private Integer part3;
    private BaseInfo baseInfo;

    public Product(String part1, String part2, Integer part3, BaseInfo baseInfo) {
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.baseInfo = baseInfo;
    }

    public Product() {
    }

    public BaseInfo getBaseInfo() {
        return baseInfo;
    }

    public void setBaseInfo(BaseInfo baseInfo) {
        this.baseInfo = baseInfo;
    }

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


    @Override
    protected Product clone() throws CloneNotSupportedException {
        Product clone = (Product) super.clone();
        BaseInfo clone1 = this.baseInfo.clone();
        clone.setBaseInfo(clone1);
        return clone;

        /*//v2  序列化实现深拷贝
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try (ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);) {
            oos.writeObject(this);
        } catch (IOException e) {
            e.printStackTrace();
        }

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

        try (ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream);) {
            Product product = (Product) ois.readObject();
            return product;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;*/
    }
}

class BaseInfo implements Cloneable, Serializable {
    private static final long serialVersionUID = 41L;
    private String companyName;

    public BaseInfo(String companyName) {
        this.companyName = companyName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    @Override
    public String toString() {
        return super.hashCode() + "BaseInfo{" +
                "companyName='" + companyName + '\'' +
                '}';
    }

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

 

posted @ 2021-05-19 00:33  炼金术士0z  阅读(40)  评论(0编辑  收藏  举报