原型模式prototype
-通过new产生一个对象需要非常繁琐的数据准备访问权限,则可以使用原型模式。
-就是Java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点
-优势有:效率高(直接克隆,避免了重新执行构造过程步骤)。
-克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出的对象属性
值完全和原型对象相同。并且克隆出的新对象改变不会影响原型对象。然后,在修改克隆对象的值。
原型模式实现:
-Cloneable借口和clone方法
-Prototype模式中实现起来最困难的地方就是内存复制操作,所幸在Java中提供了clone()方法
替我们做了绝大部分事情。
开发中的应用场景
-原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂
方法提供给调用者。
spring中bean的创建实际就是两种:单例模式和原型模式。(当然,原型模式需要和工厂模式搭配起来)
以下,请主要对比时间
/**
* 原型模式之浅克隆
* @author 万河归海
*
*/
public class Sheep implements Cloneable{
private String name;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();//直接调用object中的clone方法
return obj;
}
public Sheep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public Sheep() {
}
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;
}
}
public class Test {
public static void main(String[] args) {
try {
Date date = new Date(23123131654564L);
Sheep s1 = new Sheep("少利",date);
Sheep s2 = (Sheep)s1.clone();//克隆
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
date.setTime(122111441L);//此处更改时间
System.out.println(s1.getBirthday());
System.out.println(s2);
s2.setName("多利");
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
/**
* 原型模式之深克隆
* @author 万河归海
*
*/
public class Sheep2 implements Cloneable{
private String name;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();//直接调用object中的clone方法
//添加以下代码完成深克隆
Sheep2 s = (Sheep2)obj;
s.birthday = (Date)this.birthday.clone();//把属性别进行复制
return obj;
}
public Sheep2(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public Sheep2() {
}
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;
}
}
public class Test2 {
public static void main(String[] args) {
try {
Date date = new Date(23123131654564L);
Sheep2 s1 = new Sheep2("少利",date);
Sheep2 s2 = (Sheep2)s1.clone();//克隆
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
date.setTime(122111441L);//此处更改时间
System.out.println(s1.getBirthday());
System.out.println(s2);
s2.setName("多利");
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
以下做了解(通过序列化反序列化实现深克隆)
/**
* 原型模式之序列化反序列化成深克隆
* @author 万河归海
*
*/
public class Sheep3 implements Cloneable,Serializable{
private String name;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();//直接调用object中的clone方法
return obj;
}
public Sheep3(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public Sheep3() {
}
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;
}
}
public class Test3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Date date = new Date(23123131654564L);
Sheep3 s1 = new Sheep3("少利",date);
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
System.out.println("------------");
//使用序列化和反序列化实现深克隆
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);
Sheep3 s2 = (Sheep3)ois.readObject(); //克隆好的对象
date.setTime(122111441L);//此处更改时间
System.out.println(s1.getBirthday());
System.out.println("------------");
System.out.println(s2);
s2.setName("多利");
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
}
}