原型模式: 用一个已经创建的实例作为原型,通过复制该原型对象来创建一个和原型对象相同的新对象。
原型模式包含如下角色:
接口类图如下:

原型模式的克隆分为浅克隆和深克隆。
浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。
深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。
Java中的Object类中提供了 clone() 方法来实现浅克隆。 Cloneable 接口是上面的类图中的抽象原型类,而实现了Cloneable接口的子实现类就是具体的原型类。代码如下:
Realizetype(具体的原型类):
public class Realizetype implements Cloneable {
public Realizetype() {
System.out.println("具体的原型对象创建完成!");
}
@Override
protected Realizetype clone() throws CloneNotSupportedException {
System.out.println("具体原型复制成功!");
return (Realizetype) super.clone();
}
}
client(测试访问类):
package com.itheima.pattern.prototype.demo;
/**
* @version v1.0
* @ClassName: client
* @Description: TODO(一句话描述该类的功能)
* @Author: 黑马程序员
*/
public class client {
public static void main(String[] args) throws CloneNotSupportedException {
//创建一个原型类对象
Realizetype realizetype = new Realizetype();
//调用Realizetype类中的clone方法进行对象的克隆
Realizetype clone = realizetype.clone();
System.out.println("原型对象和克隆出来的是否是同一个对象?" + (realizetype == clone));
}
}
结果: false

用原型模式生成“三好学生”奖状。
同一学校的“三好学生”奖状除了获奖人姓名不同,其他都相同,可以使用原型模式复制多个“三好学生”奖状出来,然后在修改奖状上的名字即可。
类图如下:

package com.itheima.pattern.prototype.test;
/**
* @version v1.0
* @ClassName: Citation
* @Description: TODO(一句话描述该类的功能)
* @Author: 黑马程序员
*/
public class Citation implements Cloneable {
/*//三好学生上的姓名
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}*/
private Student stu;
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
@Override
public Citation clone() throws CloneNotSupportedException {
return (Citation) super.clone();
}
public void show() {
System.out.println(stu.getName() + "同学:在2020学年第一学期中表现优秀,被评为三好学生。特发此状!");
}
}
上面这个是浅拷贝,如果要深拷贝按照如下这种方式实现:
import java.util.ArrayList;
import java.util.List;
// 原型接口
public interface Prototype extends Cloneable {
Prototype clone();
}
// 具体原型类
public class ConcretePrototype implements Prototype {
private List<String> fields;
public ConcretePrototype(List<String> fields) {
this.fields = fields;
}
public void setFields(List<String> fields) {
this.fields = fields;
}
public List<String> getFields() {
return fields;
}
@Override
public Prototype clone() {
try {
ConcretePrototype cloned = (ConcretePrototype) super.clone();
cloned.fields = new ArrayList<>(this.fields); // 深拷贝
return cloned;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
@Override
public String toString() {
return "ConcretePrototype{fields=" + fields + "}";
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
List<String> fieldList = new ArrayList<>();
fieldList.add("Field1");
fieldList.add("Field2");
ConcretePrototype prototype1 = new ConcretePrototype(fieldList);
ConcretePrototype prototype2 = (ConcretePrototype) prototype1.clone();
prototype2.getFields().add("Field3");
System.out.println(prototype1);
System.out.println(prototype2);
}
}
package com.itheima.pattern.prototype.test;
/**
* @version v1.0
* @ClassName: Student
* @Description: TODO(一句话描述该类的功能)
* @Author: 黑马程序员
*/
public class Student {
//学生的姓名
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
package com.itheima.pattern.prototype.test;
/**
* @version v1.0
* @ClassName: CitaionTest
* @Description: TODO(一句话描述该类的功能)
* @Author: 黑马程序员
*/
public class CitaionTest {
public static void main(String[] args) throws CloneNotSupportedException {
//1,创建原型对象
Citation citation = new Citation();
//创建张三学生对象
Student stu = new Student();
stu.setName("张三");
citation.setStu(stu);
//2,克隆奖状对象
Citation citation1 = citation.clone();
Student stu1 = citation1.getStu();
stu1.setName("李四");
/*citation.setName("张三");
citation1.setName("李四");*/
//3,调用show方法展示
citation.show();
citation1.show();
}
}
clone() 方法只会进行浅拷贝,需要手动实现深拷贝。