设计模式中,单例模式应该是大家最为熟悉的了,那如果我们需要对一个对象进行多次复制的话,大家会用什么呢?这就要用到今天要讲的原型模式了。

简介

其定义为:

使用原型实例指定将要创建的对象类型,通过复制这个实例创建新的对象。

具体来说就是,通过给出一个原型对象来指明所创建的对象的类型,然后使用自身实现的克隆接口来复制这个原型对象,该模式就是用这种方式来创建出更多同类型的对象。

这样的好处是:

Object 类的 clone() 方法是一个本地方法,它可以直接操作内存中的二进制流,所以性能相对 new 实例化来说,更加优秀。

一个对象通过 new 实例化创建过程为:

  1. 在内存中开辟一块空间。
  2. 在开辟的内存空间中创建对象。
  3. 调用对象的构造函数进行初始化对象。

而一个对象通过 clone() 创建过程为:

  1. 根据原对象内存大小开辟一块内存空间。
  2. 复制已有对象,克隆对象中所有属性值。

相对 new 来说,clone() 少了调用构造函数。如果构造函数中存在大量属性初始化或大对象,则使用 clone() 的复制对象的方式性能会好一些。

简单例子

让我们通过一个例子来具体了解一下:

/**
 * 实现Cloneable 接口的原型抽象类Prototype
 */
public class Prototype implements Cloneable {
    /**
     * 重写 clone() 方法
     */
    @Override
    public Prototype clone() {
        Prototype prototype = null;
        try {
            prototype = (Prototype) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return prototype;
    }
}

/**
 * 实现原型类
 */
public class ConcretePrototype extends Prototype {
    public void show() {
        System.out.println("原型模式实现类");
    }
}

/**
 * 测试类
 */
public class Client {
    public static void main(String[] args) {
        ConcretePrototype cp = new ConcretePrototype();
        for (int i = 0; i < 10; i++) {
            ConcretePrototype cloneCp = (ConcretePrototype) cp.clone();
            cloneCp.show();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

当我们实现原型抽象类时,需要注意三点:

  1. 实现 Cloneable 接口:Cloneable 接口与序列化接口的作用类似,它只是告诉虚拟机可以安全地在实现了这个接口的类上使用 clone() 方法。在 JVM 中,只有实现了 Cloneable 接口的类才可以被拷贝,否则会抛出 CloneNotSupportedException 异常。
  2. 重写 Object 类中的 clone() 方法:在 Java 中,所有类的父类都是 Object 类,而 Object 类中有一个 clone() 方法,作用是返回对象的一个拷贝。
  3. 在重写的 clone() 方法中调用 super.clone():默认情况下,类不具备复制对象的能力,需要调用 super.clone() 来实现。

深拷贝与浅拷贝

谈到了拷贝,就不得不说到一个经典的问题:深拷贝与浅拷贝,有的地方也叫深克隆与浅克隆

在上面的原型模式中,在调用 super.clone() 方法之后,首先会检查当前对象所属的类是否支持 clone,也就是看该类是否实现了 Cloneable 接口。

如果支持,则创建当前对象所属类的一个新对象,并对该对象进行初始化,使得新对象的成员变量的值与当前对象的成员变量的值一模一样,但对于其它对象的引用以及 List 等类型的成员属性,则只能复制这些对象的引用了。所以简单调用 super.clone() 这种克隆对象方式,就是一种浅拷贝

为了让大家更加清楚浅拷贝的弊端,举个具体的例子:

Student 类中有一个 Teacher 对象,我们让这两个类都实现 Cloneable 接口:

@Getter
@Setter
public class Student implements Cloneable{

    /**
     * 学生姓名
     */
    private String name;

    /**
     * 学生所属的老师
     */
    private Teacher teacher;

    /**
     * 重写克隆方法,对学生进行克隆
     */
    public Student clone() {
        Student student = null;
        try {
            student = (Student) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return student;
    }
}

@Getter
@Setter
public class Teacher implements Cloneable{

    /**
     * 老师姓名
     */
    private String name;

    /**
     * 重写克隆方法,对老师类进行克隆
     */
    public Teacher clone() {
        Teacher teacher= null;
        try {
            teacher= (Teacher) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return teacher;
    }
}
@Getter
@Setter
public class Student implements Cloneable{

    /**
     * 学生姓名
     */
    private String name;

    /**
     * 学生所属的老师
     */
    private Teacher teacher;

    /**
     * 重写克隆方法,对学生进行克隆
     */
    public Student clone() {
        Student student = null;
        try {
            student = (Student) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return student;
    }
}

@Getter
@Setter
public class Teacher implements Cloneable{

    /**
     * 老师姓名
     */
    private String name;

    /**
     * 重写克隆方法,对老师类进行克隆
     */
    public Teacher clone() {
        Teacher teacher= null;
        try {
            teacher= (Teacher) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return teacher;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.

测试的时候,我们先定义一个学生和一个老师,并让其关联在一起。然后复制之前的学生,生成一个新的学生,修改新学生的老师。

public class Test {
    public static void main(String args[]) {
        // 定义老师1
        Teacher teacher = new Teacher();
        teacher.setName("刘老师");
        // 定义学生1
        Student stu1 = new Student();
        stu1.setName("test1");
        // 老师1和学生1进行关联
        stu1.setTeacher(teacher);

        // 复制学生1,生成学生2
        Student stu2 = stu1.clone();
        stu2.setName("test2");
        // 修改学生2的老师
        stu2.getTeacher().setName("王老师");

        // 查看修改结果
        System.out.println("学生" + stu1.getName() + "的老师是:" + stu1.getTeacher().getName());
        System.out.println("学生" + stu1.getName() + "的老师是:" + stu2.getTeacher().getName());
    }
}
public class Test {
    public static void main(String args[]) {
        // 定义老师1
        Teacher teacher = new Teacher();
        teacher.setName("刘老师");
        // 定义学生1
        Student stu1 = new Student();
        stu1.setName("test1");
        // 老师1和学生1进行关联
        stu1.setTeacher(teacher);

        // 复制学生1,生成学生2
        Student stu2 = stu1.clone();
        stu2.setName("test2");
        // 修改学生2的老师
        stu2.getTeacher().setName("王老师");

        // 查看修改结果
        System.out.println("学生" + stu1.getName() + "的老师是:" + stu1.getTeacher().getName());
        System.out.println("学生" + stu1.getName() + "的老师是:" + stu2.getTeacher().getName());
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

我们想要的结果是:

学生test1的老师是:刘老师
学生test2的老师是:王老师
  • 1.
  • 2.

但实际结果是:

学生test1的老师是:王老师
学生test2的老师是:王老师
  • 1.
  • 2.

观察以上运行结果,我们可以发现:在我们给学生2修改老师的时候,学生1的老师也跟着被修改了。这就是浅拷贝带来的问题。

我们可以通过深拷贝的方式解决这类问题,修改 Student 类的 clone() 方法:

/**
     * 重写克隆方法,对学生和老师都进行克隆
     */
    public Student clone() {
        Student student = null;
        try {
            student = (Student) super.clone();
            // 克隆 teacher 对象
            Teacher teacher = this.teacher.clone();
            student.setTeacher(teacher);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return student;
    }
    /**
     * 重写克隆方法,对学生和老师都进行克隆
     */
    public Student clone() {
        Student student = null;
        try {
            student = (Student) super.clone();
            // 克隆 teacher 对象
            Teacher teacher = this.teacher.clone();
            student.setTeacher(teacher);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return student;
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

此时,我们再次运行 Test 中的 main() 方法,就可以得到我们预想的结果了。

适用场景

在一些重复创建对象的场景下,我们就可以使用原型模式来提高对象的创建性能。例如:循环体内创建对象时,我们就可以考虑用 clone() 的方式来实现。

除此之外,原型模式在开源框架中的应用也非常广泛。例如 Spring 中,@Service 默认都是单例的。用了私有全局变量,若不想影响下次注入或每次上下文获取 bean,就需要用到原型模式,我们可以通过以下注解来实现,@Scope("prototype")。有兴趣的朋友深入了解一下其中的原理。

总结

原型模式,就是针对需要大量复制同一对象的场景,比如用户获取商品、循环体内创建对象等,都是不错的选择,且效率好。

 https://death00.github.io/