原型模式跟其他的创建型模式不同,它要做的是对特定的对象进行克隆。所谓克隆就是根据当前对象的特征,完全的复制一份出来。原型模式分为深拷贝和浅拷贝。不管是深拷贝和浅拷贝对于对象中的基本数据类型和String类型都会完全的复制,区别就是在复制对象中的其他引用类型时,浅拷贝只会复制对象中引用类型的地址,而不会完全的克隆一份。
浅拷贝
下面测试代码,User类中有Integer类型和String类型以及一个Father对象。User类对外提供的copy()方法使用了Object的clone()方法,就完成了对象的复制。需要注意的是,使用clone()方法的类需要实现Cloneable接口,该接口只是一个标记接口。最终的测试我们改变了克隆出来的对象的三个属性,原对象的Integer和String类型属性没有收到影响,而Father属性则收到了影响。这表明,克隆出来的对象并没有完全的复制,对于引用类型只是复制了其栈的引用。
package prototype_k;/* * @auther 顶风少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-15 17:49 * @notify * @version 1.0 */ public class User implements Cloneable { private String name; private Integer age; private Father father; public User(String name, Integer age, Father father) { this.name = name; this.age = age; this.father = father; } public User copy() throws Exception { return (User) this.clone(); } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setFather(Father father) { this.father = father; } public String getName() { return name; } public Integer getAge() { return age; } public Father getFather() { return father; } }