首页 > 其他 > 详细

设计模式-原型模式

时间:2021-05-19 10:18:00      阅读:18      评论:0      收藏:0      [点我收藏+]

- 原型模式

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

 

设计模式-原型模式

原文:https://www.cnblogs.com/chenfx/p/14783411.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!