23种设计模式之原型模式

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

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

开发中的应用场景
-原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂
方法提供给调用者。
spring中bean的创建实际就是两种:单例模式和原型模式。(当然,原型模式需要和工厂模式搭配起来)

以下,请主要对比时间

/**
 * 原型模式之浅克隆
 * @author 万河归海
 *
 */
public class Sheep implements Cloneable{
	private String name;
	private Date birthday;
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Object obj = super.clone();//直接调用object中的clone方法
		return obj;
	}
	public Sheep(String name, Date birthday) {
		super();
		this.name = name;
		this.birthday = birthday;
	}
	public Sheep() {
	
	}
	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;
	}
	
}
public class Test {
	public static void main(String[] args) {
		try {
			Date date = new Date(23123131654564L);
			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(122111441L);//此处更改时间
			System.out.println(s1.getBirthday());
			System.out.println(s2);
			s2.setName("多利");
			System.out.println(s2.getName());
			System.out.println(s2.getBirthday());
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
}

浅克隆

/**
 * 原型模式之深克隆
 * @author 万河归海
 *
 */
public class Sheep2 implements Cloneable{
	private String name;
	private Date birthday;
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Object obj = super.clone();//直接调用object中的clone方法
		//添加以下代码完成深克隆
		Sheep2 s = (Sheep2)obj;
		s.birthday = (Date)this.birthday.clone();//把属性别进行复制
		return obj;
	}
	public Sheep2(String name, Date birthday) {
		super();
		this.name = name;
		this.birthday = birthday;
	}
	public Sheep2() {
	
	}
	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;
	}
	
}
public class Test2 {
	public static void main(String[] args) {
		try {
			Date date = new Date(23123131654564L);
			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(122111441L);//此处更改时间
			System.out.println(s1.getBirthday());
			System.out.println(s2);
			s2.setName("多利");
			System.out.println(s2.getName());
			System.out.println(s2.getBirthday());
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
}

深克隆
以下做了解(通过序列化反序列化实现深克隆)

/**
 * 原型模式之序列化反序列化成深克隆
 * @author 万河归海
 *
 */
public class Sheep3 implements Cloneable,Serializable{
	private String name;
	private Date birthday;
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Object obj = super.clone();//直接调用object中的clone方法
		return obj;
	}
	public Sheep3(String name, Date birthday) {
		super();
		this.name = name;
		this.birthday = birthday;
	}
	public Sheep3() {
	
	}
	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;
	}
	
}
public class Test3 {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Date date = new Date(23123131654564L);
		Sheep3 s1 = new Sheep3("少利",date);
		
		System.out.println(s1);
		System.out.println(s1.getName());
		System.out.println(s1.getBirthday());
		System.out.println("------------");
		//使用序列化和反序列化实现深克隆
		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);
		
		Sheep3 s2 = (Sheep3)ois.readObject(); //克隆好的对象
		
		date.setTime(122111441L);//此处更改时间
		System.out.println(s1.getBirthday());
		System.out.println("------------");
		System.out.println(s2);
		s2.setName("多利");
		System.out.println(s2.getName());
		System.out.println(s2.getBirthday());
	}
}

通过序列化反序列化实现的深克隆

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值