前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式-原型模式

设计模式-原型模式

作者头像
用户5927264
发布2019-08-19 17:07:36
3530
发布2019-08-19 17:07:36
举报
文章被收录于专栏:OSChina
代码语言:javascript
复制
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;
	}

}
代码语言:javascript
复制
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());
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档