1.创建通用原型接口,定义用于拷贝/克隆的方法(clone)。
这里创建通用接口,以便后文演示两种克隆方法的实现方式,这个原型也可以创建成一个抽象类。
public interface Prototype {
public void setAttr(String attr);
public String getAttr();
public Prototype clone();
}
2.一般方式创建具体原型类:创建一个新的同类型对象,将自身的属性复制给新对象,最后返回这个新对象。
读者可能发现,这种实现方法存在很大的弊端。首先是代码量可能过大,有多少属性就需要使用多少次Setter。
同时代码缺乏灵活性,新增或删除成员属性时都需要对具体原型类的克隆方法进行修改。
public class ConcretePrototypeA implements Prototype {
private String attr;
public void setAttr(String attr) {
this.attr = attr;
}
public String getAttr() {
return this.attr;
}
public Prototype clone() {
Prototype prototype = new ConcretePrototypeA();
prototype.setAttr(this.attr);
return prototype;
}
}
3.其它方式创建具体原型类:
在Java中有一个概念,所有的Java类都继承自java.lang.Object,Object类中提供一个clone()方法,可以将Java对象复制一份并返回。
原型模式则可以利用这个方法将实现克隆的过程简单化。
需要注意的是使用Java自带的clone()方法时需要实现一个标识接口Cloneable,表示这个Java类支持被复制。
值得一提的是实现接口Cloneable只是起到标识的作用,相关用法可再深入探究。
public class ConcretePrototypeB implements Prototype, Cloneable {
private String attr;
public void setAttr(String attr) {
this.attr = attr;
}
public String getAttr() {
return this.attr;
}
public Prototype clone() {
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException exception) {
System.err.println("Not support cloneable");
}
return (Prototype) object;
}
}
4.创建用于测试的客户端类输出结果不难猜测,代码中注释对应输出内容。至此可以理解:原型模式将重复创建具体原型类的代码封装进了具体原型类的克隆方法里,
大大简化了客户端(其它场合亦是如此)多次创建同一个类型对象时的操作。这也就很好理解原型模式属于创建型模式的定义。
public class Client {
public static void main(String[] args) {
Prototype obj1 = new ConcretePrototypeA();
/*Prototype obj1 = new ConcretePrototypeB();*/
obj1.setAttr("Sunny");
Prototype obj2 = obj1.clone();
System.out.println(obj1.getAttr()); //Display:Sunny
System.out.println(obj2.getAttr()); //Display:Sunny
System.out.println(obj1 == obj2); //Display:false
}
}
二、浅克隆和深克隆
在浅克隆中,如果原型对象的成员变量是值类型,将复制一份给克隆对象;
如果原型对象的成员变量是引用类型,则将引用对象的地址复制一份给克隆对象,也就是说原型对象和克隆对象的成员变量指向相同的内存地址。
简单来说,在浅克隆中,当对象被复制时只复制它本身和其中包含的值类型的成员变量,而引用类型的成员变量并没有被复制。
在深克隆中,无论原型对象的成员变量是值类型还是引用类型,都将复制一份给克隆对象。
前文实例中的克隆方法均为浅克隆方式,以下演示深克隆方式。
1.创建一个演示用的Bean类作为原型对象的引用类形的成员变量。
public class Attachment implements Serializable {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void download() {
System.out.println("Name is " + name);
}
}
2.创建原型对象
在Java语言中,如果需要实现深克隆,可以通过序列化(Serialization)等方式来实现。
序列化就是将对象写到流的过程,写到流中的对象是原有对象的一个拷贝,而原对象仍然存在于内存中。
通过序列化实现的拷贝不仅可以复制对象本身,而且可以复制其引用的成员对象,因此通过序列化将对象写到一个流中,再从流里将其读出来,可以实现深克隆。需要注意的是能够实现序列化的对象其类必须实现Serializable接口,否则无法实现序列化操作。
public class WeeklyLog implements Serializable {
private Attachment attachment;
private String content;
public Attachment getAttachment() {
return attachment;
}
public void setAttachment(Attachment attachment) {
this.attachment = attachment;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return this.content;
}
// 使用序列化技术实现深克隆
public WeeklyLog deepClone() throws IOException, ClassNotFoundException,
OptionalDataException {
// 将对象写入流中
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bao);
oos.writeObject(this);
// 将对象从流中取出
ByteArrayInputStream bis = new ByteArrayInputStream(bao.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (WeeklyLog) ois.readObject();
}
}
3.创建用于测试的客户端类
public class Client {
public static void main(String args[]) {
WeeklyLog log_previous, log_new = null;
log_previous = new WeeklyLog();
Attachment attachment = new Attachment();
log_previous.setAttachment(attachment);
try {
log_new = log_previous.deepClone();
}catch (Exception e){
System.err.println("fail clone !");
}
System.out.println("Whether or not the same ? " //Display:false
+ (log_previous == log_new));
System.out.println("Whether or not the same ? " //Display:false
+ (log_previous.getAttachment() == log_new.getAttachment()));
}
}