zoukankan      html  css  js  c++  java
  • 设计模式——原型模式

          当创建给定的实例的过程很昂贵或非常复杂的时候,就使用原型模式来拷贝,这种方式在创建对象要耗费很多资源的时候效率提升显著。

          实现深度克隆有两种方法,一种是实现Cloneable接口,重写clone()方法。另一种是通过序列化反序列化来获取对象的拷贝。

          看一个介绍:http://blog.csdn.net/zhengzhb/article/details/7393528

           看一个Demo:

          

    public class Prototype implements Cloneable , Serializable{
    	String name;
    	Date date;
    	
    	public Prototype(String name, Date date) {
    		this.name = name;
    		this.date = date;
    	}
    	
    	public Prototype() {
    	}
    	
    	public void setDate(Date date) {
    		this.date = date;
    	}public void setName(String name) {
    		this.name = name;
    	}
        @Override
        protected Object clone() throws CloneNotSupportedException {
        	
        	return super.clone();
        }
        
        protected Object deepClone() throws CloneNotSupportedException {
        	Object obj = this.clone();
        	Prototype p = (Prototype) obj;
        	p.date = (Date) this.date.clone();
        	return obj;
        }
        
    }
    
    public class ClientOne {
    	
    	public static void shallowClone() throws Exception {
    		Date date = new Date(12356565656L);
    		Prototype p1 = new Prototype("原型对象", date);
    		Prototype p2 = (Prototype) p1.clone();
    		System.out.println(p1);
    		System.out.println(p1.date);
    		
    		//p1.setDate(new Date(323232326565656L));
    		date.setTime(36565656562626L);
    		System.out.println(p2);
    		System.out.println(p2.date);
    	}
    	
    	public static void deepClone() throws Exception {
    		Date date = new Date(12356565656L);
    		Prototype p1 = new Prototype("原型对象",date);
    		Prototype p2 = (Prototype) p1.deepClone();
    		System.out.println(p1);
    		System.out.println(p1.date);
    		date.setTime(36565656562626L);
    		System.out.println(p2);
    		System.out.println(p2.date);
    	}
    	
    	public static void deepCloneSerialize() throws Exception {
    		Date date = new Date(12356565656L);
    		Prototype p1 = new Prototype("原型对象",date);
    		//通过序列化反序列化来新建一个对象
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		ObjectOutputStream    oos = new ObjectOutputStream(bos);
    		oos.writeObject(p1);
    		byte[] bytes = bos.toByteArray();
    		
    		ByteArrayInputStream  bis = new ByteArrayInputStream(bytes);
    		ObjectInputStream	  ois = new ObjectInputStream(bis);
    		Prototype p2 = (Prototype) ois.readObject();
    		System.out.println(p1);
    		System.out.println(p1.date);
    		date.setTime(36565656562626L);
    		System.out.println(p2);
    		System.out.println(p2.date);
    	}
    	
    	public static void main(String[] args) throws Exception {
    		System.out.println("Shallow clone:");
    		shallowClone();
    		System.out.println("Deep clone:");
    		deepClone();
    		System.out.println("Deep clone serialize:");
    		deepCloneSerialize();
    		System.exit(0);
    	}
    
    }
    
  • 相关阅读:
    Norton我错怪了你啊~~
    RUNRMTCMD命令使用
    如何查看QTEMP的内容?可以查看别人的QTEMP的
    关于文件的ShareODP和USROPN
    虚拟主机权限之log4net
    如何向远程系统提交命令?
    在5250上面实现复制粘贴
    php与数据库对应实体类的命名
    Action Script 中的 super
    Linux下源码编译方式安装MySQL5.5.12(转)
  • 原文地址:https://www.cnblogs.com/wxisme/p/4540634.html
Copyright © 2011-2022 走看看