设计模式之三原型模式

基本介绍

设计模式之三原型模式

 

 类图 

设计模式之三原型模式

 

代码演示

package com.hy.prototype.improve;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author hanyong
 * @date 2020/11/6 23:12
 */

@Data
@AllArgsConstructor
public class Sheep implements Cloneable {
    private String name;
    private int age;
    private String color;


    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sheep;
    }
}

package com.hy.prototype.improve;

/**
 * @author hanyong
 * @date 2020/11/6 23:21
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("原型模式完成对象的创建");
        // TODO Auto-generated method stub
        Sheep sheep = new Sheep("tom", 1, "白色");


        Sheep sheep2 = (Sheep) sheep.clone(); //克隆
        Sheep sheep3 = (Sheep) sheep.clone(); //克隆
        Sheep sheep4 = (Sheep) sheep.clone(); //克隆
        Sheep sheep5 = (Sheep) sheep.clone(); //克隆

        System.out.println(sheep==sheep2);
        System.out.println(sheep.equals(sheep2));
        System.out.println(sheep+""+sheep.hashCode());
        System.out.println(sheep2+""+sheep2.hashCode());
        System.out.println(sheep3+""+sheep3.hashCode());
        System.out.println(sheep4);
        System.out.println(sheep5);
    }
}

当对象有属性非基本类型而是对象的时候,拷贝不会对对象属性进行拷贝,而是引用初始对象的的属性对象的地址

代码演示

package com.hy.prototype.improve;

import lombok.*;

/**
 * @author hanyong
 * @date 2020/11/6 23:12
 */


@AllArgsConstructor
@ToString
public class Sheep implements Cloneable {
    @Setter
    @Getter
    private String name;
    @Setter
    @Getter
    private int age;
    @Setter
    @Getter
    private String color;
    public Sheep freend ;

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

    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sheep;
    }
}

package com.hy.prototype.improve;

/**
 * @author hanyong
 * @date 2020/11/6 23:21
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("原型模式完成对象的创建");
        // TODO Auto-generated method stub
        Sheep sheep = new Sheep("tom", 1, "白色");
        sheep.freend = new Sheep("小友", 16, "白色");

        Sheep sheep2 = (Sheep) sheep.clone(); //克隆
        Sheep sheep3 = (Sheep) sheep.clone(); //克隆
        Sheep sheep4 = (Sheep) sheep.clone(); //克隆
        Sheep sheep5 = (Sheep) sheep.clone(); //克隆

        System.out.println(sheep == sheep2);
        System.out.println(sheep.equals(sheep2));
        System.out.println(sheep + "" + sheep.hashCode()+"spfr:"+sheep.freend.hashCode());
        System.out.println(sheep2 + "" + sheep2.hashCode()+"spfr2:"+sheep2.freend.hashCode());
        System.out.println(sheep3 + "" + sheep3.hashCode());
        System.out.println(sheep4);
        System.out.println(sheep5);
    }
}

输出结果

 设计模式之三原型模式

 

 复制后friend hashcode指向同一个地址,并没有进行复制,以上属于浅拷贝,非基本数据类型的对象不会进行复制而是指向地址

深拷贝,解决浅拷贝存在问题,会对非基本类型属性进行拷贝。实现方式两种1.重写clone方法 2.使用序列化来实现深拷贝

代码演示两种拷贝方式

package com.hy.prototype.deepclone;

import java.io.Serializable;

/**
 * @author hanyong
 * @date 2020/11/7 9:24
 */
public class DeepCloneableTarget implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;
    private String cloneName;

    private String cloneClass;

    //构造器
    public DeepCloneableTarget(String cloneName, String cloneClass) {
        this.cloneName = cloneName;
        this.cloneClass = cloneClass;
    }

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


package com.hy.prototype.deepclone;

import java.io.*;

/**
 * @author hanyong
 * @date 2020/11/7 9:27
 */
public class DeepProtoType implements Serializable, Cloneable {

    public String name; //String 属性
    public DeepCloneableTarget deepCloneableTarget;// 引用类型

    public DeepProtoType() {
        super();
    }


    //深拷贝 - 方式 1 使用clone 方法
    @Override
    protected Object clone() throws CloneNotSupportedException {

        Object deep = null;
        deep = super.clone();
        DeepProtoType deepProtoType = (DeepProtoType) deep;
        deepProtoType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
        return deepProtoType;
    }

    //深拷贝 - 方式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 copyObj = (DeepProtoType) ois.readObject();
            return copyObj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            //关闭流
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            } catch (Exception e2) {
                // TODO: handle exception
                System.out.println(e2.getMessage());
            }
        }

    }

}


package com.hy.prototype.deepclone;

/**
 * @author hanyong
 * @date 2020/11/7 9:33
 */
public class Client {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        DeepProtoType p = new DeepProtoType();
        p.name = "宋江";
        p.deepCloneableTarget = new DeepCloneableTarget("大牛", "小牛");

        //方式1 完成深拷贝

//        DeepProtoType p2 = (DeepProtoType) p.clone();
//
        /*System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());
        System.out.println("p2.name=" + p2.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());*/

        //方式2 完成深拷贝
        DeepProtoType p2 = (DeepProtoType) p.deepClone();

        System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());
        System.out.println("p2.name=" + p2.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());

    }

}

测试结果

设计模式之三原型模式

 

上一篇:c++菱形继承


下一篇:设计模式之原型模式