设计模式-原型模式

package com.shi.design.prototype;

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

/**
 * @author shiye
 *
 */
public class Sleep implements Cloneable,Serializable{
	
	private String name;
	
	private String age;
	
	private String add;
	
	//对象中有一个引用对象类型的羊
	private Sleep friend;

	@Override
	public String toString() {
		return "Sleep [name=" + name + ", age=" + age + ", add=" + add + ", friend=" + friend + ", getName()="
				+ getName() + ", getAge()=" + getAge() + ", getAdd()=" + getAdd() + ", getFriend()=" + getFriend()
				+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
				+ "]";
	}


	public Sleep(String name, String age, String add, Sleep friend) {
		super();
		this.name = name;
		this.age = age;
		this.add = add;
		this.friend = friend;
	}


	public String getName() {
		return name;
	}


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


	public String getAge() {
		return age;
	}


	public void setAge(String age) {
		this.age = age;
	}


	public String getAdd() {
		return add;
	}


	public void setAdd(String add) {
		this.add = add;
	}


	public Sleep getFriend() {
		return friend;
	}


	public void setFriend(Sleep friend) {
		this.friend = friend;
	}

	//深拷贝的俩重方式
	//方式一 :使用自己重写父方法colne方法实现克隆
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Sleep sleep = (Sleep)super.clone();//实现浅拷贝
		if(sleep.getFriend() != null) {
			sleep.setFriend((Sleep)sleep.getFriend().clone()); //引用对象需要自己实现拷贝
		}
		return sleep;
	}
	
	// 方式二:通过序列化的方式实现深拷贝 (推荐使用 效率高)
	public Object deepClone() {
		
		Sleep copyObj = null;
		
		//1 创建流对象
		ByteArrayOutputStream bos = null;
		ObjectOutputStream oos = null;
		ByteArrayInputStream bis = null;
		ObjectInputStream ois = null;
		
		try {
			//2 序列化
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(this);
			
			//3 反序列化
			bis = new ByteArrayInputStream(bos.toByteArray());
			ois = new ObjectInputStream(bis);
			copyObj = (Sleep)ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(bos != null ) {
					bos.close();
				}
				if(oos != null ) {
					oos.close();
				}
				if(bis != null ) {
					bis.close();
				}
				if(ois != null ) {
					ois.close();
				}
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
		return copyObj;
	}

}
package com.shi.design.prototype;

public class Test1 {

	public static void main(String[] args) throws CloneNotSupportedException {
		Sleep sleep1 = new Sleep("小阳","12","地球村",new Sleep("朋友", "10", "不确定", null));
		Sleep sleep2 = (Sleep) sleep1.clone();
		
		System.out.println("使用 方式一 实现的深拷贝~~");
		System.out.println("sleep1.hashCode() = " + sleep1.hashCode() + "/sleep1.getFriend().hashCode() = " + sleep1.getFriend().hashCode());
		System.out.println("sleep2.hashCode() = " + sleep2.hashCode() + "/sleep2.getFriend().hashCode() = " + sleep2.getFriend().hashCode());
	
		System.out.println("使用 方式二 实现的深拷贝~~");
		Sleep sleep3 = (Sleep)sleep1.deepClone();
		System.out.println("sleep3.hashCode() = " + sleep3.hashCode() + "/sleep3.getFriend().hashCode() = " + sleep3.getFriend().hashCode());
	}

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券