原型模式初探
- 通过new产生一个对象需要复杂的数据准备或访问权限,可以使用原型模式。
- 就是Java的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点
- 优势有:效率高(直接克隆,避免了重新执行构造函数的过程)
- 克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出来的对象属性完全与原型对象相同。而且克隆出来的对象的属性值的修改不会影响到原型对象
原型模式的实现
- Clonable接口和clone方法
- prototype模式中实现起来最困难的部分就是内存复制操作,所幸Java中提供了clone()方法来帮助我们做了绝大部分事情
- 注意用词:克隆和拷贝时一样的
开发当中的使用场景
- 原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂方法提供给调用者。
- spring中bean的创建市级就是两种:单例模式和原型模式
浅复制例子
package com.coderbean.prototype;
import java.util.Date;
public class Sheep implements Cloneable {
private String name;
private Date birthday;
public Sheep() {
}
public Sheep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
return obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package com.coderbean.prototype
import java.util.Date
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(123456L)
Sheep s1 = new Sheep("ShaoLi", date)
System.out.println(s1)
System.out.println(s1.getName())
System.out.println(s1.getBirthday())
Sheep s2 = (Sheep) s1.clone()
System.out.println(s2)
date.setTime(999999L)
System.out.println(s1.getName())
System.out.println(s1.getBirthday())
System.out.println(s2.getName())
System.out.println(s2.getBirthday())
}
}
深复制举例
package com.coderbean.prototype;
import java.util.Date;
public class Sheep2 implements Cloneable {
private String name;
private Date birthday;
public Sheep2() {
}
public Sheep2(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
Sheep2 s = (Sheep2) obj;
s.setBirthday((Date)this.birthday.clone());
return obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package com.coderbean.prototype;
import java.util.Date;
/**
* 测试原型模式(深克隆)
* @author Chang
*
*/
public class Client2 {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(123456L);
Sheep2 s1 = new Sheep2("ShaoLi", date);
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
Sheep2 s2 = (Sheep2) s1.clone();
System.out.println(s2);
date.setTime(999999L);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
}
}
利用序列化和反序列化技术实现深克隆
package com.coderbean.prototype;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* 测试原型模式(深克隆,使用序列化和反序列化实现)
* @author Chang
*
*/
public class Client3 {
public static void main(String[] args) throws CloneNotSupportedException, ClassNotFoundException, IOException {
Date date = new Date(123456L);
Sheep s1 = new Sheep("ShaoLi", date);
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(s1);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
Sheep s2 = (Sheep) ois.readObject();
date.setTime(1234562342L);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
}
}