原型模式(用一个已经创建的实例作为原型,通过复制该原型对象来创建一个和原型相同或相似的新对象)
系统中,存在大量相同或相似对象的创建问题,如果用传统的构造函数来创建对象,会比较复杂且耗时耗资源,用原型模式生成对象就很高效
- 优点
Java 自带的原型模式基于内存二进制流的复制,在性能上比直接 new 一个对象更加优良。
可以使用深克隆方式保存对象的状态,使用原型模式将对象复制一份,并将其状态保存起来,简化了创建对象的过程,以便在需要的时候使用(例如恢复到历史某一状态),可辅助实现撤销操作。 - 缺点
需要为每一个类都配置一个 clone 方法
clone 方法位于类的内部,当对已有类进行改造的时候,需要修改代码,违背了开闭原则。
当实现深克隆时,需要编写较为复杂的代码,而且当对象之间存在多重嵌套引用时,为了实现深克隆,每一层对象对应的类都必须支持深克隆,实现起来会比较麻烦。因此,深克隆、浅克隆需要运用得当。
实现(浅克隆和深克隆)
- 浅克隆:是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。
Java 中的 Object 类提供了浅克隆的 clone() 方法,具体原型类只要实现 Cloneable 接口就可实现对象的浅克隆,这里的 Cloneable 接口就是抽象原型类。其代码如下:
public class Student implements Cloneable {
String name;
int age;
public Student(String name, int age) {
System.out.println("Student 对象已创建");
this.name = name;
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
System.out.println("Student 对象被克隆");
return super.clone();
}
@Override
public String toString() {
return "{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
public class Teacher implements Cloneable {
String name;
Student student;
public Teacher(String name, Student student) {
System.out.println("Teacher 对象已创建");
this.name = name;
this.student = student;
}
@Override
protected Object clone() throws CloneNotSupportedException {
System.out.println("Teacher 对象被克隆");
return super.clone();
}
@Override
public String toString() {
return "{" + "name='" + name + '\'' + ", student=" + student.toString() + '}';
}
}
public class Test {
public static void main(String[] args) throws Exception {
// Student对象
Student student1 = new Student("张三", 20);
// Teacher对象
Teacher teacher1 = new Teacher("李老师", student1);
// Teacher新对象
Teacher teacher2 = (Teacher)teacher1.clone();
teacher2.name = "张老师";
System.out.println("teacher1:" + teacher1.toString());
System.out.println("teacher2:" + teacher2.toString());
}
}
输出结果如下:
Student对象已创建
Teacher对象已创建
Teacher对象被克隆
teacher1:{name='李老师', student={name='李四', age=20}}
teacher2:{name='张老师', student={name='李四', age=20}}
当改变Teacher新对象中 name 属性值后,Teacher对象中的值并未跟着一块改变,现在执行下面代码
public class Test {
public static void main(String[] args) throws Exception {
// Student对象
Student student1 = new Student("张三", 20);
// Teacher对象
Teacher teacher1 = new Teacher("李老师", student1);
// Teacher新对象
Teacher teacher2 = (Teacher)teacher1.clone();
teacher2.name = "张老师";
teacher2.student.name = "李四";
teacher2.student.age = 10;
System.out.println("teacher1:" + teacher1.toString());
System.out.println("teacher2:" + teacher2.toString());
}
}
输出结果如下:
Student对象已创建
Teacher对象已创建
Teacher对象被克隆
teacher1:{name='李老师', student={name='李四', age=10}}
teacher2:{name='张老师', student={name='李四', age=10}}
当改变Teacher新对象中 Student对象中的name和age 属性值后,Teacher对象中的student中的值跟着一块改变。但是我们在实际应用中并不想Teacher对象中的student中的值跟着一块改变,则可以进行深克隆的操作
- 深克隆:不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。
public class Student implements Cloneable {
String name;
int age;
public Student(String name, int age) {
System.out.println("Student 对象已创建");
this.name = name;
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
System.out.println("Student 对象被克隆");
return super.clone();
}
@Override
public String toString() {
return "{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
public class Teacher implements Cloneable {
String name;
Student student;
public Teacher(String name, Student student) {
System.out.println("Teacher 对象已创建");
this.name = name;
this.student = student;
}
@Override
protected Object clone() throws CloneNotSupportedException {
System.out.println("Teacher对象被克隆");
Teacher teacher = (Teacher)super.clone();
teacher.student = (Student)teacher.student.clone();
return teacher;
}
@Override
public String toString() {
return "{" + "name='" + name + '\'' + ", student=" + student.toString() + '}';
}
}
public class Test {
public static void main(String[] args) throws Exception {
// Student对象
Student student1 = new Student("张三", 20);
// Teacher对象
Teacher teacher1 = new Teacher("李老师", student1);
// Teacher新对象
Teacher teacher2 = (Teacher)teacher1.clone();
teacher2.name = "张老师";
System.out.println("teacher1:" + teacher1.toString());
System.out.println("teacher2:" + teacher2.toString());
}
}
输出结果如下:
Student对象已创建
Teacher对象已创建
Teacher对象被克隆
Student对象被克隆
teacher1:{name='李老师', student={name='张三', age=20}}
teacher2:{name='张老师', student={name='李四', age=10}}
- 应用场景
1.对象之间相同或相似,即只是个别的几个属性不同的时候。
2.创建对象成本较大,例如初始化时间长,占用CPU太多,或者占用网络资源太多等,需要优化资源。
3.创建一个对象需要繁琐的数据准备或访问权限等,需要提高性能或者提高安全性。
4.系统中大量使用该类对象,且各个调用者都需要给它的属性重新赋值。
JSON工具类中的 JSON.parseObject() 运用到了原型模式;