【源码分析设计模式 4】JDK中的原型模式

this.age = age;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

@Override

public String toString() {

return “Sheep [name=” + name + “, age=” + age + “, color=” + color + “, address=” + address + “]”;

}

//克隆该实例,使用默认的clone方法来完成

@Override

protected Object clone() {

Sheep sheep = null;

try {

sheep = (Sheep)super.clone();

} catch (Exception e) {

System.out.println(e.getMessage());

}

return sheep;

}

}

package designMode.advance.prototype;

public class Client {

public static void main(String[] args) {

System.out.println(“原型模式完成对象的创建”);

Sheep sheep = new Sheep(“tom”, 1, “白色”);

sheep.friend = new Sheep(“jack”, 2, “黑色”);

Sheep sheep2 = (Sheep)sheep.clone(); //克隆

Sheep sheep3 = (Sheep)sheep.clone(); //克隆

Sheep sheep4 = (Sheep)sheep.clone(); //克隆

Sheep sheep5 = (Sheep)sheep.clone(); //克隆

System.out.println(“sheep2 =” + sheep2 + “sheep2.friend=” + sheep2.friend.hashCode());

System.out.println(“sheep3 =” + sheep3 + “sheep3.friend=” + sheep3.friend.hashCode());

System.out.println(“sheep4 =” + sheep4 + “sheep4.friend=” + sheep4.friend.hashCode());

System.out.println(“sheep5 =” + sheep5 + “sheep5.friend=” + sheep5.friend.hashCode());

}

}

2、深拷贝代码实例

package designMode.advance.prototype;

import java.io.Serializable;

public class DeepCloneableTarget implements Serializable, Cloneable {

private static final long serialVersionUID = 1L;

private String cloneName;

private String cloneClass;

//构造器

public DeepCloneableTarget(String cloneName, String cloneClass) {

this.cloneName = cloneName;

this.cloneClass = cloneClass;

}

//因为该类的属性,都是String , 因此我们这里使用默认的clone完成即可

@Override

protected Object clone() throws CloneNotSupportedException {

return super.clone();

}

}

package designMode.advance.prototype;

import java.io.*;

public class DeepProtoType implements Serializable, Cloneable {

public String name; //String 属性

public DeepCloneableTarget deepCloneableTarget;// 引用类型

public DeepProtoType() {

super();

}

//深拷贝 - 通过对象的序列化实现 (推荐)

public Object deepClone() {

//创建流对象

ByteArrayOutputStream bos = null;

ObjectOutputStream oos = null;

ByteArrayInputStream bis = null;

ObjectInputStream ois = null;

try {

//序列化

bos = new ByteArrayOutputStream();

oos = new ObjectOutputStream(bos);

oos.writeObject(this); //当前这个对象以对象流的方式输出

//反序列化

bis = new ByteArrayInputStream(bos.toByteArray());

ois = new ObjectInputStream(bis);

DeepProtoType copyObj = (DeepProtoType)ois.readObject();

return copyObj;

} catch (Exception e) {

e.printStackTrace();

return null;

} finally {

//关闭流

try {

bos.close();

oos.close();

bis.close();

ois.close();

} catch (Exception e2) {

System.out.println(e2.getMessage());

}

}

}

}

package designMode.advance.prototype;

public class Client {

public static void main(String[] args) {

// TODO Auto-generated method stub

DeepProtoType p = new DeepProtoType();

p.name = “宋江”;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值