设计模式-原型模式

Posted 铁匠转码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式-原型模式相关的知识,希望对你有一定的参考价值。

- 原型模式

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

 

以上是关于设计模式-原型模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式之原型模式(Prototype)详解及代码示例

设计模式课程 设计模式精讲 9-2 原型模式coding

设计模式--原型模式

设计模式原型模式

Java设计模式----原型模式

设计模式--------原型模式