Java设计模式原型模式

 原型模式:

– 通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。

– 就是java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点

– 优势有:效率高(直接克隆,避免了重新执行构造过程步骤) 。

– 克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出的 对象的属性值完全和原型对象相同。并且克隆出的新对象改变不会影响原型对象。然后, 再修改克隆对象的值。 

原型模式实现:

– Cloneable接口和clone方法

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

 

import java.util.Date;

public class Sheep implements Cloneable {
	private String sname;
	private Date birthday;
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Object obj=super.clone();
		
		//实现深复制
		Sheep s=(Sheep) obj;
		s.birthday=(Date)this.birthday.clone();
		
		return obj;//直接调用object对象的clone(); 
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Sheep(String sname, Date birthday) {
		super();
		this.sname = sname;
		this.birthday = birthday;
	}
	public Sheep(){
		
	}
}

  

import java.util.Date;

public class Client {  //浅克隆
	public static void main(String[] args) throws Exception{
		Date d=new Date(12312321331L);
		Sheep s1=new Sheep("baodan",d);
		System.out.println(s1);
		System.out.println(s1.getSname());
		System.out.println(s1.getBirthday());
		
		d.setTime(22234335L);
		
		System.out.println(s1.getBirthday());
		
		Sheep s2=(Sheep)s1.clone();
		s2.setSname("xiaobaodan");
		System.out.println(s2);
		System.out.println(s2.getSname());
		System.out.println(s2.getBirthday());
	}
}

  

import java.util.Date;

//深复制
public class Client2 {
	public static void main(String[] args) throws CloneNotSupportedException {
		Date d=new Date(12312321331L);
		Sheep s1=new Sheep("baodan",d);
		Sheep s2=(Sheep)s1.clone(); //深复制
		System.out.println(s1);
		System.out.println(s1.getSname());
		System.out.println(s1.getBirthday());
		
		d.setTime(22234335L);
		
		System.out.println(s1.getBirthday());
		//Sheep s2=(Sheep)s1.clone();//浅复制
		
		s2.setSname("xiaobaodan");
		System.out.println(s2);
		System.out.println(s2.getSname());
		System.out.println(s2.getBirthday());
	}
}

  序列化和反序列化实现深克隆。

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);
		
		Sheep s2=(Sheep)ois.readObject();

   通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式

       如果需要短时间创建大量对象,并且new的过程比较耗时。则可以考虑使用原型模式!

public class Client4 {
	public static void testNew(int size){
		long start = System.currentTimeMillis();
		for(int i=0;i<size;i++){
			Laptop t = new Laptop();
		}
		long end = System.currentTimeMillis();
		System.out.println("new的方式创建耗时:"+(end-start));
	}
	
	public static void testClone(int size) throws CloneNotSupportedException{
		long start = System.currentTimeMillis();
		Laptop t = new Laptop();
		for(int i=0;i<size;i++){
			Laptop temp = (Laptop) t.clone();
		}
		long end = System.currentTimeMillis();
		System.out.println("clone的方式创建耗时:"+(end-start));
	}
	
	
	public static void main(String[] args) throws Exception {	
		testNew(1000);
		testClone(1000);
	}
}


class Laptop implements Cloneable {  
	public Laptop() {
		try {
			Thread.sleep(10);  //模拟创建对象耗时的过程!
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Object obj = super.clone();  //直接调用object对象的clone()方法!
		return obj;
	}
}

  

posted @ 2019-11-24 21:15  石shi  阅读(277)  评论(0编辑  收藏  举报