原型模式小试(二)

2014-11-23 21:59:38 · 作者: · 浏览: 5
ed native java.lang.Object clone() throws java.lang.CloneNotSupportedException
  我们可以看到,这个方法是protected访问控制,同时是一个本地方法。也就是说,任何Object的子类都可以调用这个方法,但是Object对象自身不能调用这个方法。
  这个方法执行的时候,是使用RTTI(run-time type identification)的机制,动态得找到目前正在调用clone方法的那个reference,根据它的大小申请内存空间,然后进行bitwise的复制,将该对象的内存空间完全复制到新的空间中去,从而达到shallowcopy的目的。(这句话摘自百度知道)
(2)、问题:
  1、为什么调用对象要实现cloneable接口?
  因为clone方法执行的时候,会判断当前对象是否实现了Cloneable这个标识接口,如果没有实现,就抛出CloneNotSupportedException异常。
  2、为什么调用对象要重写public Object clone();方法,为什么要调用super.clone()?
  如果调用对象不重写public Object clone()方法,那么clone()方法都不会显示出来。调用clone()方法,直接编译错误。可是clone()方法不是protected的吗?
  我们来看一下clone();方法的API:
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention,
it will be the case that x.clone().getClass() == x.getClass().
--按照惯例,返回的对象应该通过调用 super.clone 获得。如果一个类及其所有的超类(Object 除外)都遵守此约定,则 x.clone().getClass() == x.getClass()
  因为只有重写clone()方法,我们才能通过使用super.clone(),才能真正调用Object类的本地方法clone();
  从这里我们可以看到,clone()方法是很特殊的。不重写,会编译错误,应该涉及到编译器的优化。
(3)、实现:  
我们来Java的原型模式简单改一下上面的示例:
1、将Fruit接口中的克隆功能去掉,这里不列出了。
2、Apple类实现Fruit接口,也实现Cloneable接口,并重写clone()方法,如下:
复制代码
1 public class Apple implements Fruit, Cloneable {
2
3 // 价格
4 private double price;
5 // 平均尺寸
6 private double avgSize;
7 // 产地
8 private String productionArea;
9
10 public Apple(double price, double avgSize, String productionArea)
11 {
12 super();
13 this.price = price;
14 this.avgSize = avgSize;
15 this.productionArea = productionArea;
16 }
17
18 public double getAvgSize()
19 {
20 return avgSize;
21 }
22
23 public void setAvgSize(double avgSize)
24 {
25 this.avgSize = avgSize;
26 }
27
28 public String getProductionArea()
29 {
30 return productionArea;
31 }
32
33 public void setProductionArea(String productionArea)
34 {
35 this.productionArea = productionArea;
36 }
37
38 @Override
39 public void setPrice(double price)
40 {
41 // TODO Auto-generated method stub
42 this.price = price;
43 }
44
45 @Override
46 public double getPrice()
47 {
48 // TODO Auto-generated method stub
49 return price;
50 }
51
52 // 重写了Object类的clone()方法
53 @Override
54 public Object clone()
55 {
56 // TODO Auto-generated method stub
57 Object cloneApple = null;
58 // 直接调用父类的克隆方法即可
59 try
60 {
61 cloneApple = super.clone();
62 } catch (CloneNotSupportedException e)
63 {
64 e.printStackTrace();
65 }
66 return cloneApple;
67 }
68
69 @Override
70 public String toString()
71 {
72 return "Apple [avgSize=" + avgSize + ", price=" + price + ", productionArea=" + productionArea + "]";
73 }
74
75 }
复制代码
3、测试一下:
复制代码
1 public class Client {
2
3 static Fruit fruit = new Apple(90, 5, "新疆");
4
5 public static void main(String[] args) {
6 System.out.println("Fruit:" + fruit);
7