原型模式

  • 通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式
  • 就是java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点
  • 优势有:效率高(直接克隆,避免了重新执行构造过程步骤)
  • 克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出的对象的属性值完全和原型对象相同。并且克隆出的新对象改变不会影响原型对象。然后,再修改克隆对象的值


原型模式实现

  • 实现Cloneable接口和clone方法
  • Prototype模式中实现起来最困难的地方就是内存复制操作,所幸在Java中提供了clone()方法替我们做了绝大部分事情


浅克隆(对象间会影响)

实体类:实现Cloneable接口和clone方法

package shejimoshi.原型模式;

import java.util.Date;

public class Sheep implements Cloneable {
private String name;
private Date birthday;

@Override
protected Object clone() throws CloneNotSupportedException {
// 直接调用object对象的clone()方法
Object obj = super.clone();
return obj;
}

public Sheep(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}

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 shejimoshi.原型模式;

import java.util.Date;

/**
* 浅克隆(对象间会影响)
*
* @author 陈霓清
*/
public class Client {

public static void main(String[] args) throws Exception {
Date date = new Date(100000000L);
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(200000000L);
System.out.println(s1.getBirthday());

s2.setName("新多利");
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
}
}

浅克隆存在的问题

  • 被复制的对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象




深度复制

实体类:把属性也进行克隆

package shejimoshi.原型模式;

import java.util.Date;

public class Sheep2 implements Cloneable {
private String name;
private Date birthday;

@Override
protected Object clone() throws CloneNotSupportedException {
// 直接调用object对象的clone()方法
Object obj = super.clone();

// 添加如下代码实现深复制(deep Clone)
Sheep2 entity = (Sheep2) obj;
entity.birthday = (Date) this.birthday.clone(); //把属性也进行克隆
return obj;
}

public Sheep2(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}

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 shejimoshi.原型模式;

import java.util.Date;

/**
* 深度复制
*
* @author 陈霓清
*/
public class Client2 {

public static void main(String[] args) throws Exception {
Date date = new Date(100000000L);
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(200000000L);
System.out.println(s1.getBirthday());

System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
}
}





利用序列化和反序列化技术实现深克隆

实体类:实现序列化接口

package shejimoshi.原型模式;

import java.io.Serializable;
import java.util.Date;

public class Sheep implements Cloneable, Serializable {
private String name;
private Date birthday;

@Override
protected Object clone() throws CloneNotSupportedException {
//直接调用object对象的clone()方法
Object obj = super.clone();
return obj;
}

public Sheep(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}

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 shejimoshi.原型模式;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
* 利用序列化和反序列化技术实现深克隆
*
* @author 陈霓清
*/
public class Client3 {

public static void main(String[] args) throws Exception {
Date date = new Date(100000000L);
Sheep s1 = new Sheep("多利", 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(200000000L);
System.out.println(s1.getBirthday());

System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
}
}


回到顶部