原型模式
原型模式概述:用一个已经创建的实例作为原型,通过复制该原型对象来创建一个和原型对象相同的新对象。
原型模式包含的角色
抽象原型类:规定了具体原型对象必须实现的clone()方法。
具体原型类:实现了抽象圆形的clone()方法,它是可被复制的对象。
访问类:使用具体原型类中的clone()方法来复制对象。
接口类图
原型模式的克隆分为浅克隆和深克隆
浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。
深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址
在JDK中的Object类中提供了clone()方法来实现浅克隆。Cloneable接口是上面类图中的抽象原型,而实现了Cloneable接口的子实现类就是具体的原型类。
public class Realizetype implements Cloneable{
public Realizetype(){
System.out.println("具体的原型对象创建完成");
}
@Override
protected Realizetype clone() throws CloneNotSupportedException {
System.out.println("聚体原型复制成功");
return (Realizetype) super.clone();
}
}
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
Process finished with exit code 0
例子:用原型模式生成“三好学生”奖状。同一学校的“三好学生”奖状除了获奖人性名不同,其他都相同,可以使用原型模式复制多个“三好学生”奖状出来,然后再修改奖状上的名字即可
案例类图
public class Citation implements Cloneable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
protected Citation clone() throws CloneNotSupportedException {
return (Citation) super.clone();
}
public void show(){
System.out.println(name+"同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状");
}
}
public class CitationTest {
public static void main(String[] args) throws CloneNotSupportedException {
//创建原型对象
Citation citation=new Citation();
//克隆奖状对象
Citation clone = citation.clone();
citation.setName("张三");
clone.setName("李四");
//调用show方法
citation.show();
clone.show();
}
}
结果
张三同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状
李四同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状
Process finished with exit code 0
原型模式使用场景
对象的创建非常复杂,可以使用原型模式快捷的创建对象;性能和安全要求比较高的时候使用原型模式
原型模式扩展——深克隆
将三好学生奖状的案例中的Citation类的name属性修改为Student类型的属性
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Citation implements Cloneable{
private Student student;
public void setStudent(Student student) {
this.student = student;
}
public Student getStudent() {
return student;
}
@Override
protected Citation clone() throws CloneNotSupportedException {
return (Citation) super.clone();
}
public void show(){
System.out.println(student.getName()+"同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状");
}
}
public class CitationTest {
public static void main(String[] args) throws CloneNotSupportedException {
//创建原型对象
Citation citation=new Citation();
//克隆奖状对象
Student student = new Student();
student.setName("张三");
citation.setStudent(student);
Citation clone = citation.clone();
clone.getStudent().setName("李四");
citation.show();
clone.show();
}
}
结果
李四同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状
李四同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状
Process finished with exit code 0
说明:这是浅克隆的结果,两个对象都是李四,对具体原型类中的引用类型的属性进行引用的复制。这种情况下要使用深克隆,而进行深克隆需要使用对象流。
public class Student implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Citation implements Cloneable, Serializable {
private Student student;
public void setStudent(Student student) {
this.student = student;
}
public Student getStudent() {
return student;
}
@Override
protected Citation clone() throws CloneNotSupportedException {
return (Citation) super.clone();
}
public void show(){
System.out.println(student.getName()+"同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状");
}
}
//创建原型对象
Citation citation=new Citation();
//创建张三学生对象
Student student = new Student();
student.setName("张三");
citation.setStudent(student);
//创建对象输出流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\test.txt"));
//写对象
oos.writeObject(citation);
//释放资源
oos.close();
//创建对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\test.txt"));
//读取对象
Citation citation1 =(Citation) ois.readObject();
//释放资源
ois.close();
Student student1=citation1.getStudent();
student.setName("李四");
citation.show();
citation1.show();
}
}
结果
李四同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状
张三同学:在2022学年第一学期中表现优秀,被评为三好学生,特发此奖状
Citation类和Student类必须实现Serializable接口,否则会抛出BotSerializableException异常。