深拷贝和浅拷贝(原型模式)

百度百科:

https://baike.baidu.com/item/%E6%B5%85%E6%8B%B7%E8%B4%9D/8648181?fr=aladdin

https://baike.baidu.com/item/%E6%B7%B1%E6%8B%B7%E8%B4%9D/22785317?fr=aladdin

浅拷贝:

深拷贝和浅拷贝(原型模式)_第1张图片

深拷贝:

深拷贝和浅拷贝(原型模式)_第2张图片

 这里推荐使用第二种方式:

代码:

package designpattern.singleton.type1;

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

import javax.imageio.spi.IIOServiceProvider;

public class DeepCopyObject implements Cloneable, Serializable{
	
	private String name ;
	private int age;
	private DeepCopyObject friend;
	
	
	public DeepCopyObject(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	public DeepCopyObject(String name, int age, DeepCopyObject friend) {
		super();
		this.name = name;
		this.age = age;
		this.friend = friend;
	}
	
	public static void main(String[] args) {
		DeepCopyObject deepCopyObject = new DeepCopyObject("小妞", 18, new DeepCopyObject("二妞", 16));
		
		DeepCopyObject deepCopyObject2 = (DeepCopyObject) deepCopyObject.deepCopy();
		
		System.out.println("源对象:" + deepCopyObject);
		System.out.println("深拷贝对象:" + deepCopyObject2);
		
		System.out.println("源对象hashCode:" +deepCopyObject.hashCode() );
		System.out.println("深拷贝对象hashCode:" +deepCopyObject2.hashCode() );
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//核心方法 通过对象的序列化实现 
	public Object deepCopy() {
		//创建流对象
		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);
			DeepCopyObject deepCopyObject = (DeepCopyObject) ois.readObject();
			return deepCopyObject;
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			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 e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
		
	}

	@Override
	public String toString() {
		return "DeepCopyObject [name=" + name + ", age=" + age + ", friend=" + friend + "]";
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

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

	public DeepCopyObject getFriend() {
		return friend;
	}

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

测试结果:

深拷贝和浅拷贝(原型模式)_第3张图片

对象的属性都一致,并且hashCode不一样,说明是不同的对象引用,也就完成了深度拷贝 

原型模式(https://baike.baidu.com/item/%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F/4941014?fr=aladdin)

本质就是完成深度拷贝!!

Spring也用到了原型模式:

深拷贝和浅拷贝(原型模式)_第4张图片

 

你可能感兴趣的:(设计模式)