原型模式其实就是对对象的拷贝,原型体现在和原来的对象完全一致。应用方面的话对于一个复杂的对象如果我们有多个方面需要对其进行修改操作并且操作之间相互隔离,此时就需要原型模型。
首先我们需要编写一个用于测试的类
package design_pattern.prototype; import java.util.Date; //实现Cloneable接口有点类似于序列化接口,只是用于标识,没有具体的方法需要实现 public class People implements Cloneable{ String name; int age; Date date; public People(String name, int age, Date date) { this.name = name; this.age = age; this.date = date; } @Override public String toString() { return "People{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; } //我们需要调用Object的clone()方法 @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }产生上述问题的原因就在于,浅拷贝对于对象的属性复制方面,基本类型时值拷贝,引用类型时引用地址拷贝,所以上述其实是指向了同一个Date对象的内存地址,所以当Date对象改变时,两个对象也对应发生了改变。
深拷贝的理念就是,先创建一个浅拷贝对象,然后通过将拷贝对象的引用属性进行再次克隆,使得两个引用属性完全独立。(通俗点说就是一个深拷贝由多次浅拷贝完成)
首先我们需要对测试类的clone()方法进行改造
package design_pattern.prototype; import java.util.Date; public class People implements Cloneable{ String name; int age; Date date; public People(String name, int age, Date date) { this.name = name; this.age = age; this.date = date; } @Override public String toString() { return "People{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; } @Override protected Object clone() throws CloneNotSupportedException{ //首先进行浅拷贝 People people = (People) super.clone(); //再对对象的属性进行再次浅拷贝 people.date = (Date) this.date.clone(); return people; } } package design_pattern.prototype; import java.util.Date; public class Deep { public static void main(String[] args) throws Exception{ Date date = new Date(); People people1 = new People("邢宇飞",21,date); People people2 = (People) people1.clone(); System.out.println("people1 = " + people1.toString()); System.out.println("people2 = " + people2.toString()); System.out.println("================================================="); date.setTime(123321123); System.out.println("people1 = " + people1.toString()); System.out.println("people2 = " + people2.toString()); } } 输出结果: people1 = People{name='邢宇飞', age=21, date=Sat Oct 03 15:00:33 CST 2020} people2 = People{name='邢宇飞', age=21, date=Sat Oct 03 15:00:33 CST 2020} ================================================= people1 = People{name='邢宇飞', age=21, date=Fri Jan 02 18:15:21 CST 1970} people2 = People{name='邢宇飞', age=21, date=Sat Oct 03 15:00:33 CST 2020}和我们想的一样,确实是两个对象是完全隔离开来了。