定义:通过拷贝一个已经存在的实例来返回新的实例,而不是新建实例。被拷贝的实例就称为原型。
类图
原型类实现思路
(1)实现Cloneable接口。(在Java虚拟机中,只有实现了这个接口的类才可以被拷贝。)
(2)重写Object类中的clone方法。(作用是返回对象的一个拷贝,但其作用域是protected,要修改成public。)
原型模式中的拷贝分为“浅拷贝”和“深拷贝”。
浅拷贝
对值类型的成员变量进行值的复制,对引用类型的成员变量只复制引用、不复制引用的对象。
深拷贝
对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制。
Object类的clone方法只会拷贝对象中的基本的数据类型,对于String类型、数组、容器对象、引用对象等都不会拷贝。如果要实现深拷贝,必须对String类型、数组、容器对象、引用对象等另行拷贝。Java提供的大部分容器类都实现了Cloneable接口。
例子
原型类
public class Prototype implements Cloneable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Prototype clone(){
Prototype prototype = null;
try{
prototype = (Prototype)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return prototype;
}
}
public class LightPrototype extends Prototype{
public void display(){
System.out.println(this.getName());
}
}
测试类
public class LightPrototypeTest {
public static void main(String[] args){
LightPrototype lightPrototype = new LightPrototype();
lightPrototype.setName("original lightPrototype!");
LightPrototype copyLightPrototypePrototype = (LightPrototype)lightPrototype.clone();
System.out.println(copyLightPrototypePrototype.getName());
}
}
public class DeepPrototype implements Cloneable{
private String id;
private Prototype prototype;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Prototype getPrototype() {
return prototype;
}
public void setPrototype(Prototype prototype) {
this.prototype = prototype;
}
@Override
public DeepPrototype clone(){
DeepPrototype deepPrototype = null;
try{
deepPrototype = (DeepPrototype)super.clone();
//对引用对象的拷贝
deepPrototype.prototype = (Prototype)this.prototype.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return deepPrototype;
}
}
测试类
public class DeepPrototypeTest {
public static void main(String[] args){
Prototype prototype = new Prototype();
prototype.setName("original Prototype!");
DeepPrototype deepPrototype = new DeepPrototype();
deepPrototype.setId("deepPrototype id!");
deepPrototype.setPrototype(prototype);
DeepPrototype copyDeepPrototypePrototype = (DeepPrototype)deepPrototype.clone();
System.out.println(copyDeepPrototypePrototype.getId());
System.out.println(copyDeepPrototypePrototype.getPrototype().getName());
}
}