`
指尖残血
  • 浏览: 2740 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

本文参考:《修炼Java开发技术:在架构中体验设计模式和算法之美   于广编著》。

 

原型模式是指用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建新的实例。也就是说,原型模式是通过复制现在已经存在的对象来创建一个新对象(类似于:孙悟空拔毫毛变出很多的猴子)。

 

 在Java中的object提供了clone方法,能够实现拷贝的Java类必须实现一个标识接口Cloneable,表示这个Java类支持被复制。如果一个类没有实现这个接口但是调用了clone()方法,Java编译器将抛出一个CloneNotSupportedException异常。

演示代码:

package org.dyb.design.prototype;

public class ConcretePrototype implements Cloneable {
	public ConcretePrototype clone(){
		Object object = null;
		try {
			object = super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return (ConcretePrototype)object;
	}
}

 使用:

package org.dyb.design.prototype;

public class Test {
	@org.junit.Test
	public void test(){
		ConcretePrototype cp = new ConcretePrototype();
		ConcretePrototype cpClone = cp.clone();
	}
}

 Java语言中的clone()方法满足下列关系:

1、对于任何对象x,都有x.clone()!=x,即拷贝对象与原型对象不是同一个对象。

2、对于任何对象x,都有x.clone().getClass == x.getClass(),拷贝对象与原型对象的类型是一样的。

3、如果对象x的equals()方法定义恰当,那么x.clone.equals(x)应该成立。

 

原型模式浅拷贝与原型模式深度拷贝:

浅拷贝:拷贝对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。换言之,浅拷贝仅仅拷贝所考虑的对象,而不拷贝它所引用的对象。

深拷贝:在浅拷贝的基础上,将引用对象指向被拷贝过的新对象,而不再是原有的那些被引用的对象。

package org.dyb.design.prototype;

public class Apple {
	private String color;

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	
}

 

package org.dyb.design.prototype;

public class Thing implements Cloneable {
	
	private String attr;
	private Apple apple;//对象
	@Override
	public Thing clone(){
		Thing t = null;
		try {
			t = (Thing)super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	public String getAttr() {
		return attr;
	}
	public void setAttr(String attr) {
		this.attr = attr;
	}
	public Apple getApple() {
		return apple;
	}
	public void setApple(Apple apple) {
		this.apple = apple;
	}
	
}

 测试:

package org.dyb.design.prototype;

public class Test {
	@org.junit.Test
	public void test(){
		Apple a = new Apple();
		a.setColor("red");
		Thing t1 = new Thing();
		t1.setApple(a);
		t1.setAttr("x");
		Thing t2 = t1.clone();
		t2.getApple().setColor("green");
		t2.setAttr("y");
		
		System.out.println(t1.getAttr());
		System.out.println(t2.getAttr());
		System.out.println(t1.getApple().getColor());
		System.out.println(t2.getApple().getColor());
	}
}

 结果:

x
y
green
green
属性attr在t1和t2中不相同,因为我们进行了修改,但是在对象apple中t1和t2用的是同一个引用,并没有重新将apple实例化一个新的对象。这个就是浅拷贝。

 

改动后Thing:

package org.dyb.design.prototype;

public class Thing implements Cloneable {
	
	private String attr;
	private Apple apple;
	@Override
	public Thing clone(){
		Thing t = null;
		try {
			t = (Thing)super.clone();
			if(this.apple != null){
				Apple a = new Apple();
				a.setColor(this.apple.getColor());
				t.setApple(a);
			}
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	public String getAttr() {
		return attr;
	}
	public void setAttr(String attr) {
		this.attr = attr;
	}
	public Apple getApple() {
		return apple;
	}
	public void setApple(Apple apple) {
		this.apple = apple;
	}
	
}

 测试结果:

x
y
red
green

 

应用:

当某公司一次性发送上千万个邮件的时候,如果我们使用单线程每个邮件0.01秒,那么也至少需要27个小时,如果使用多线程则会将同一个引用的数据进行修改,如上例子,这个时候就使用圆形模式,保证每个发送的内容都是一个新的对象,互不影响。

 

注意:对象拷贝的时候构造函数式没有执行的。原理:从内存中(堆内存)以二进制流的方式进行拷贝,重新分配一个内存块。那构造函数没有执行就很正常了。

 

应用场景:

1、类初始化需要加载非常多的资源。

2、通过new产生一个对象需要非常繁琐的数据准备或访问权限。

3、一个对象多个修改者的场景。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics