原型设计模式

原型设计模式

一、原型介绍

原型模式是创建型设计模式。

通过原型实例指定创建对象的种类,并且通过拷贝原型,创建新的对象。

原型类A,原型实例A1,通过拷贝原型实例A1,创建多个A的原型实列A2,A3…

类似于西游记孙悟空拔出猴毛,变出与其要一模一样的孙悟空。

我有类A,我new 了一个实例a1。当我想要再创建跟a1一样的实例时,我们最简单的做法就是继续new一个实例a2,然后将a1中的属性获取并赋值给a2,这样我们就得到了一个与a1一样的实例a2。

但是这样的做法如果遇到new的对象比较复杂时,效率很低。而且总是重新初始化对象,而不是动态的获得对象运行时的状态,不够灵活

二、Java中的原型使用

在Java中Object类是所有类的根类,Object类提供了一个clone()方法,该方法可以将一个Java对象复制一份,但是需要实现clone的Java类必须要实现一个接口Cloneable,该接口表示该类能够复制且具有复制的能力

三、普通方式拷贝对象

通过new创建一个新的对象(羊),然后将原型实例(懒羊羊)的属性值赋值到新的对象(羊),这样就成功拷贝(克隆)了一个新的懒羊羊

/**
 * @Author: dashu
 * @Description: 羊
 * @Date:  22:52
 * @Version: 1.0
 */
public class Sheep {

    /**
     * 名字
     */
    private String name;

    /**
     * 年龄
     */
    private int age;

    /**
     * 颜色
     */
    private String colour;


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


    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 getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }


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

}

/**
 * @Author: dashu
 * @Description: 克隆羊
 * @Date:  22:55
 * @Version: 1.0
 */
public class CloneSheep {

    public static void main(String[] args) {

        Sheep sheep = new Sheep("懒羊羊", 12, "白色");

        Sheep sheep1 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColour());
        Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColour());
        Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColour());
        sheep.setColour("黑色");
        Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColour());
        Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColour());

        System.out.println("---------------------------------------");
        System.out.println("原型懒羊羊hashCode:"+sheep.hashCode());
        System.out.println(sheep.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊1号hashCode:"+sheep1.hashCode());
        System.out.println(sheep1.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊2号hashCode:"+sheep2.hashCode());
        System.out.println(sheep2.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊3号hashCode:"+sheep3.hashCode());
        System.out.println(sheep3.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊4号hashCode:"+sheep4.hashCode());
        System.out.println(sheep4.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊5号hashCode:"+sheep5.hashCode());
        System.out.println(sheep5.toString());
        System.out.println("---------------------------------------");


    }

}

四、Java原型拷贝

原型实现一个接口Cloneable,重写方法clone

以后原型实例的拷贝,只需调用方法clone就可以动态的拷贝创建一个新的对象

/**
 * @Author: dashu
 * @Description: 羊
 * @Date: 22:52
 * @Version: 1.0
 */
public class Sheep  implements Cloneable{

    /**
     * 名字
     */
    private String name;

    /**
     * 年龄
     */
    private int age;

    /**
     * 颜色
     */
    private String colour;


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


    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 getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }


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

    /**
	*浅拷贝
	*/
    @Override
    protected Sheep clone() throws CloneNotSupportedException {
        Sheep sheep = (Sheep) super.clone();
        return sheep;
    }


}
/**
 * @Author: dashu
 * @Description: 克隆羊
 * @Date: 22:55
 * @Version: 1.0
 */
public class CloneSheep{

    public static void main(String[] args) throws CloneNotSupportedException {

        Sheep sheep = new Sheep("懒羊羊", 12, "白色");

        Sheep sheep1 = sheep.clone();
        Sheep sheep2 = sheep.clone();
        Sheep sheep3 = sheep.clone();
        Sheep sheep4 = sheep.clone();
        Sheep sheep5 = sheep.clone();


        System.out.println("---------------------------------------");
        System.out.println("原型懒羊羊hashCode:"+sheep.hashCode());
        System.out.println(sheep.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊1号hashCode:"+sheep1.hashCode());
        System.out.println(sheep1.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊2号hashCode:"+sheep2.hashCode());
        System.out.println(sheep2.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊3号hashCode:"+sheep3.hashCode());
        System.out.println(sheep3.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊4号hashCode:"+sheep4.hashCode());
        System.out.println(sheep4.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊5号hashCode:"+sheep5.hashCode());
        System.out.println(sheep5.toString());
        System.out.println("---------------------------------------");



    }

}

五、浅拷贝和深拷贝

1、浅拷贝

对数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将属性值复制一份给新的对象。

而对于数据类型是引用数据类型的成员变量,比如说成员变量是一个数组、一个集合或某个类的对象等,那么浅拷贝会进行引用(内存地址)传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。因为实际上两个对象的成员变量都指向同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另外一个对象该成员变量值

目录4(Java原型拷贝),克隆羊案例就是浅拷贝

浅拷贝默认使用clone方法来实现

2、深拷贝

(1)实现接口:Cloneable
/**
 * @Author: dashu
 * @Description: 狼
 * @Date: 2022/3/30 23:31
 * @Version: 1.0
 */
public class Wolf implements Cloneable{

    /**
     * 名字
     */
    private String name;

    /**
     * 年龄
     */
    private int age;

    /**
     * 颜色
     */
    private String colour;



    public Wolf(String name, int age, String colour) {
        this.name = name;
        this.age = age;
        this.colour = colour;
    }


    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 getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }


    @Override
    public String toString() {
        return "[" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", colour='" + colour + '\'' +
                ']';
    }

    @Override
    protected Wolf clone() throws CloneNotSupportedException {
        Wolf wolf = (Wolf) super.clone();
        return wolf;
    }
}
/**
 * @Author: dashu
 * @Description: 羊
 * @Date: 22:52
 * @Version: 1.0
 */
public class Sheep implements Cloneable{

    /**
     * 名字
     */
    private String name;

    /**
     * 年龄
     */
    private int age;

    /**
     * 颜色
     */
    private String colour;

    /**
     * 狼朋友
     */
    private Wolf wolf;





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


    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 getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }


    public Wolf getWolf() {
        return wolf;
    }

    public void setWolf(Wolf wolf) {
        this.wolf = wolf;
    }


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


    /**
	*深拷贝
	*/
    @Override
    protected Sheep clone() throws CloneNotSupportedException {
        Sheep sheep = (Sheep) super.clone();
        Wolf wolf = sheep.getWolf();
        Wolf wolf2 = wolf.clone();
        sheep.setWolf(wolf2);
        return sheep;
    }


}
/**
 * @Author: dashu
 * @Description: 克隆羊
 * @Date: 22:55
 * @Version: 1.0
 */
public class CloneSheep {

    public static void main(String[] args) throws CloneNotSupportedException {

        Sheep sheep = new Sheep("懒羊羊", 12, "白色");
        sheep.setWolf(new Wolf("小灰灰",6,"灰色"));
        Sheep sheep1 = sheep.clone();
        Sheep sheep2 = sheep.clone();
        Sheep sheep3 = sheep.clone();
        Sheep sheep4 = sheep.clone();
        Sheep sheep5 = sheep.clone();


        System.out.println("---------------------------------------");
        System.out.println("原型懒羊羊hashCode:"+sheep.hashCode() + "-原型小灰灰hashCode:"+sheep.getWolf().hashCode());
        System.out.println(sheep.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊1号hashCode:"+sheep1.hashCode() + "-克隆小灰灰hashCode:"+sheep1.getWolf().hashCode());
        System.out.println(sheep1.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊2号hashCode:"+sheep2.hashCode() + "-克隆小灰灰hashCode:"+sheep2.getWolf().hashCode());
        System.out.println(sheep2.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊3号hashCode:"+sheep3.hashCode() + "-克隆小灰灰hashCode:"+sheep3.getWolf().hashCode());
        System.out.println(sheep3.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊4号hashCode:"+sheep4.hashCode() + "-克隆小灰灰hashCode:"+sheep4.getWolf().hashCode());
        System.out.println(sheep4.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊5号hashCode:"+sheep5.hashCode() + "-克隆小灰灰hashCode:"+sheep5.getWolf().hashCode());
        System.out.println(sheep5.toString());
        System.out.println("---------------------------------------");



    }

}
(2)实现接口:Serializable
/**
 * @Author: dashu
 * @Description: 狼
 * @Date: 2022/3/30 23:31
 * @Version: 1.0
 */
public class Wolf implements Serializable {

    /**
     * 名字
     */
    private String name;

    /**
     * 年龄
     */
    private int age;

    /**
     * 颜色
     */
    private String colour;


    public Wolf(String name, int age, String colour) {
        this.name = name;
        this.age = age;
        this.colour = colour;
    }


    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 getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }


    @Override
    public String toString() {
        return "[" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", colour='" + colour + '\'' +
                ']';
    }


}
/**
 * @Author: dashu
 * @Description: 羊
 * @Date: 22:52
 * @Version: 1.0
 */
public class Sheep implements Serializable {

    /**
     * 名字
     */
    private String name;

    /**
     * 年龄
     */
    private int age;

    /**
     * 颜色
     */
    private String colour;

    /**
     * 狼朋友
     */
    private Wolf wolf;


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


    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 getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }


    public Wolf getWolf() {
        return wolf;
    }

    public void setWolf(Wolf wolf) {
        this.wolf = wolf;
    }


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

	/**
	*深拷贝
	*/
    protected Sheep clone() {

        Sheep sheep = null;

        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);
            sheep = (Sheep) ois.readObject();
        } catch (ClassNotFoundException | IOException exception) {
            exception.printStackTrace();
        } finally {

        }


        return sheep;
    }


}
/**
 * @Author: dashu
 * @Description: 克隆羊
 * @Date: 22:55
 * @Version: 1.0
 */
public class CloneSheep {

    public static void main(String[] args) throws CloneNotSupportedException {

        Sheep sheep = new Sheep("懒羊羊", 12, "白色");
        sheep.setWolf(new Wolf("小灰灰",6,"灰色"));
        Sheep sheep1 = sheep.clone();
        Sheep sheep2 = sheep.clone();
        Sheep sheep3 = sheep.clone();
        Sheep sheep4 = sheep.clone();
        Sheep sheep5 = sheep.clone();


        System.out.println("---------------------------------------");
        System.out.println("原型懒羊羊hashCode:"+sheep.hashCode() + "-原型小灰灰hashCode:"+sheep.getWolf().hashCode());
        System.out.println(sheep.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊1号hashCode:"+sheep1.hashCode() + "-克隆小灰灰hashCode:"+sheep1.getWolf().hashCode());
        System.out.println(sheep1.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊2号hashCode:"+sheep2.hashCode() + "-克隆小灰灰hashCode:"+sheep2.getWolf().hashCode());
        System.out.println(sheep2.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊3号hashCode:"+sheep3.hashCode() + "-克隆小灰灰hashCode:"+sheep3.getWolf().hashCode());
        System.out.println(sheep3.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊4号hashCode:"+sheep4.hashCode() + "-克隆小灰灰hashCode:"+sheep4.getWolf().hashCode());
        System.out.println(sheep4.toString());
        System.out.println("---------------------------------------");
        System.out.println("克隆懒羊羊5号hashCode:"+sheep5.hashCode() + "-克隆小灰灰hashCode:"+sheep5.getWolf().hashCode());
        System.out.println(sheep5.toString());
        System.out.println("---------------------------------------");



    }

}
(3)测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值