Java设计模式之原型模式

定义:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.

Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建

浅复制

被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。

深复制

被复制对象的所有的变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把重复的对象所引用的对象都复制一遍,而这种对被引用到的对象的复制叫做间接复制。



浅复制

Mokey.JAVA

package desin.Prototype.lower;

import java.util.Date;



public class Mokey implements Cloneable {

private int height;
private int weight;
private Date birthDate;
private GoldRingdeStaff goldRingdeStaff;

public Mokey(){
birthDate= new Date();
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public Object clone(){
Mokey mokey=null;
try {
mokey=(Mokey)super.clone();
} catch (CloneNotSupportedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
finally{
return mokey;
}
}

public GoldRingdeStaff getGoldRingdeStaff() {
return goldRingdeStaff;
}

public void setGoldRingdeStaff(GoldRingdeStaff goldRingdeStaff) {
this.goldRingdeStaff = goldRingdeStaff;
}

}
GoldRingdeStaff。JAVA

package desin.Prototype.lower;

public class GoldRingdeStaff {
private float height=100.0f;
private float weight=10.0f;
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}

}

testClient.JAVA

package desin.Prototype.lower;

public class testClient {
private Mokey mokey= new Mokey();

public void change(){
Mokey copymokey2;
copymokey2=(Mokey)mokey.clone();
System.out.println("monkey birth date :"+mokey.getBirthDate());
System.out.println("copymokey2 birth date :"+copymokey2.getBirthDate());
System.out.println("copymokey2 ==monkey :"+(copymokey2==mokey));
System.out.println("copymokey2 staff ==monkey staff:"+(copymokey2.getGoldRingdeStaff()==mokey.getGoldRingdeStaff()));
}


public static void main(String[] args) {
// TODO 自动生成方法存根
testClient testClient= new testClient();
testClient.change();

}

}



深复制

Mokey.JAVA

package desin.Prototype.deep;

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

import com.sun.corba.se.internal.io.OptionalDataException;


public class Mokey implements Serializable, Cloneable {
private int height;
private int weight;
private Date birthDate;
private GoldRingdeStaff goldRingdeStaff;

public Mokey(){
this.birthDate=new Date();
this.goldRingdeStaff=new GoldRingdeStaff();
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public GoldRingdeStaff getGoldRingdeStaff() {
return goldRingdeStaff;
}

public void setGoldRingdeStaff(GoldRingdeStaff goldRingdeStaff) {
this.goldRingdeStaff = goldRingdeStaff;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public Object deepClone() throws IOException,OptionalDataException,ClassNotFoundException{

ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo= new ObjectOutputStream(bo);
oo.writeObject(this);

ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi= new ObjectInputStream(bi);
return (oi.readObject());
}

}

GoldRingdeStaff.JAVA

package desin.Prototype.deep;

import java.io.Serializable;



public class GoldRingdeStaff implements Cloneable, Serializable {
private float height=100.0f;
private float weight=10.0f;
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
testClient.JAVA

package desin.Prototype.deep;

import java.io.IOException;

import com.sun.corba.se.internal.io.OptionalDataException;


public class testClient {
private Mokey mokey= new Mokey();

public void change()throws IOException,OptionalDataException,ClassNotFoundException{
Mokey copymokey2;
copymokey2=(Mokey)mokey.deepClone();
System.out.println("monkey birth date :"+mokey.getBirthDate());
System.out.println("copymokey2 birth date :"+copymokey2.getBirthDate());
System.out.println("copymokey2 ==monkey :"+(copymokey2==mokey));
System.out.println("copymokey2 staff ==monkey staff:"+(copymokey2.getGoldRingdeStaff()==mokey.getGoldRingdeStaff()));
}


public static void main(String[] args) throws IOException,OptionalDataException,ClassNotFoundException{
// TODO 自动生成方法存根
testClient testClient= new testClient();
testClient.change();

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值