您的位置:首页 > 编程语言 > Go语言

GOF23设计模式——原型模式(通过系列化克隆的深克隆)

2018-08-16 02:58 495 查看
/**
* 测试对象
*
* @author Administrator
*
*/
public class Name implements Cloneable, Serializable{

private static final long serialVersionUID = 1L;

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Name clone() {
try {
return (Name)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
/**
* 深克隆
*
* @author Administrator
*
*/
public class ProductByDeepClone implements Cloneable, Serializable{

private static final long serialVersionUID = 1L;

private String id;

private Name name;

private String[] part;

public ProductByDeepClone(String id, Name name, String[] part) {
super();
this.id = id;
this.name = name;
this.part = part;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Name getName() {
return name;
}

public void setName(Name name) {
this.name = name;
}

public String[] getPart() {
return part;
}

public void setPart(String[] part) {
this.part = part;
}

/**
*  深克隆
*/
public ProductByDeepClone clone() {
try {
ProductByDeepClone p = (ProductByDeepClone)super.clone();
p.name = this.name.clone();
p.part = this.part.clone();
return p;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
ublic class ClientBySerializable {

public static void main(String[] args) throws IOException, ClassNotFoundException {
String id = "1";
Name name = new Name();
name.setName("name1");
String[] part = { "part1", "part2" };
ProductByDeepClone p1 = new ProductByDeepClone(id, name, part);
System.out.println("首次赋值");
System.out.println(p1);
System.out.println(p1.getId());
System.out.println(p1.getName().getName());
System.out.println(p1.getPart()[0]);
System.out.println();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(p1);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
ProductByDeepClone p2 = (ProductByDeepClone) ois.readObject();
// 克隆
System.out.println("克隆后的值");
System.out.println(p2);
System.out.println(p2.getId());
System.out.println(p2.getName().getName());
System.out.println(p2.getPart()[0]);
System.out.println();

id = "2";
name.setName("name2");
part[0] = "part21";

// 给原对象重新赋值后原先的对象的值
System.out.println("给原对象重新赋值后原先的对象的值");
System.out.println(p1);
System.out.println(p1.getId());
System.out.println(p1.getName().getName());
System.out.println(p1.getPart()[0]);
System.out.println();

System.out.println("给原对象重新赋值后新的对象的值");
System.out.println(p2);
System.out.println(p2.getId());
System.out.println(p2.getName().getName());
System.out.println(p2.getPart()[0]);
System.out.println();
}
}

运行结果:

首次赋值
com.suntefan.model.prototype.ProductByDeepClone@7852e922
1
name1
part1

克隆后的值
com.suntefan.model.prototype.ProductByDeepClone@568db2f2
1
name1
part1

给原对象重新赋值后原先的对象的值
com.suntefan.model.prototype.ProductByDeepClone@7852e922
1
name2
part21

给原对象重新赋值后新的对象的值
com.suntefan.model.prototype.ProductByDeepClone@568db2f2
1
name1
part1
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: