下面我们来浅谈原型模式,这里通过浅克隆和深克隆来说明
其实我们spring的bean中 scope=‘prototype’ ,这里其实就是原型模式,可以实例化多个对象
浅克隆:简单一句,就是引用对象,没有被克隆!
深克隆:就是引用对象也被克隆了
实体类:
package com.asiainfo.prototype;
import java.io.Serializable;
public class Person implements Serializable{
private String username;
private int age;
public Person() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
浅克隆:
package com.asiainfo.prototype;
import java.util.Date;
public class FlowMoney implements Cloneable{
private String type;//钱的种类
private String score;//额度
private Person person;
public FlowMoney() {
}
public FlowMoney(String type, String score,Person person) {
this.type = type;
this.score = score;
this.person=person;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public Object clone() throws CloneNotSupportedException {
FlowMoney o=null;
try
{
o=(FlowMoney)super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
return o;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
深克隆:
package com.asiainfo.prototype;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class DeepMoney implements Cloneable,Serializable{
private String type;//钱的种类
private String score;//额度
private Person person;
public DeepMoney() {
}
public DeepMoney(String type, String score,Person person) {
this.type = type;
this.score = score;
this.person=person;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public Object clone() throws CloneNotSupportedException {
//通过序列化与反序列化 实现深克隆,将引用对象也克隆到新对象中
Object obj = null;
//序列化
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectInputStream ois = null;
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
//反序列化
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bis);
obj= ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
测试:
<pre name="code" class="html">package com.asiainfo.test;
import com.asiainfo.prototype.DeepMoney;
import com.asiainfo.prototype.FlowMoney;
import com.asiainfo.prototype.Person;
public class TestPrototype {
public static void main(String[] args) throws Exception {
//浅克隆,引用对象是不能克隆的
Person person = new Person();
person.setAge(22);
person.setUsername("zengml");
FlowMoney money1 = new FlowMoney("RMB","100",person);
FlowMoney money2= (FlowMoney) money1.clone();
//修改引用对象的值
money1.getPerson().setAge(23);
money1.getPerson().setUsername("zengml1");
System.out.println(money1.getPerson().getUsername());
System.out.println(money2.getPerson().getUsername());
//判断引用对象是否相等
System.out.println("引用对象是否相等:"+(money1.getPerson()==money2.getPerson()));
//深克隆
Person depPerson = new Person();
depPerson.setAge(22);
depPerson.setUsername("zengml");
DeepMoney money3 = new DeepMoney("RMB","100",depPerson);
//克隆对象
DeepMoney money4= (DeepMoney) money3.clone();
//修改引用对象的值
money3.getPerson().setAge(23);
money3.getPerson().setUsername("zengml1");
System.out.println(money3.getPerson().getUsername());
System.out.println(money4.getPerson().getUsername());
System.out.println("引用对象是否相等:"+(money3.getPerson()==money4.getPerson()));
}
}
结果:
zengml1
zengml1
引用对象是否相等:true
zengml1
zengml
引用对象是否相等:false
这样就很简单明了,深克隆与浅克隆的区别了!