设计模式-原型模式

上传人:自*** 文档编号:48497413 上传时间:2018-07-16 格式:PPT 页数:27 大小:445.54KB
返回 下载 相关 举报
设计模式-原型模式_第1页
第1页 / 共27页
设计模式-原型模式_第2页
第2页 / 共27页
设计模式-原型模式_第3页
第3页 / 共27页
设计模式-原型模式_第4页
第4页 / 共27页
设计模式-原型模式_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《设计模式-原型模式》由会员分享,可在线阅读,更多相关《设计模式-原型模式(27页珍藏版)》请在金锄头文库上搜索。

1、设计模式 原型(Prototype)模式 Session3 By 佘丹1st Jul 2008原型(Prototype)模式通过给出一个原型对象来指明所要创建的对象类型,然后用拷贝 这个原型对象的办法创建出更多的同类型对象。 孙大圣的毫毛孙悟空在与黄风怪的战斗中,“使一个身外身的手段:把毫毛揪下 一把,用口嚼得粉碎,望上一喷,叫声变,变有百十个行者, 都是一样得打扮,各执一根铁棒,把那怪围在空中。”换而言之, 孙悟空可以根据自己的形象,拷贝出很多“身外身”来。孙悟空这种身外身的手段在面向对象设计领域里叫原型( Prototype)模式。1,Java对原型模式的支持 在Java里面,我们可以通过

2、Clone()方法实现原型模式。任何类,只要 想支持克隆,必须实现Cloneable接口。Cloneable接口中有Clone方法 ,可以在类中复写实现自定义的克隆方法。克隆的实现方法有三种:浅 拷贝、深拷贝和完全拷贝。(1)浅拷贝 被拷贝对象的所有变量都含有与原来的对象相同的值,而所有的对其他 对象的引用仍然指向原来的对象。换言之,浅拷贝仅仅拷贝所考虑的对 象,而不拷贝它所引用的对象。(2)深拷贝被拷贝对象的所有变量都含有与原来的对象相同的值,除去那些引用其 他对象的变量。那些引用其他对象的变量将指向被拷贝过的新对象,而 不再是原有的那些被引用的对象。换言之,深拷贝把要拷贝的对象所引 用的对

3、象都拷贝了一遍。 (3)完全拷贝完全拷贝不仅把要拷贝的对象所直接引用的对象都拷贝了一遍, 还把该 对象间接引用到的所有对象也都拷贝了一遍。这是最彻底的一种拷贝。2,Java的clone()方法 Clone protected Object clone() throws CloneNotSupportedException This method may be called to create a new copy of the Object. The typical behavior is as follows: o = o.clone() is false o.getClass() = o.

4、clone().getClass() is true o.equals(o) is true However, these are not strict requirements, and may be violated if necessary. Of the three requirements, the last is the most commonly violated, particularly if the subclass does not override equals(Object) 55 . If the Object you call clone() on does no

5、t implement Cloneable (which is a placeholder interface), then a CloneNotSupportedException is thrown. Notice that Object does not implement Cloneable; this method exists as a convenience for subclasses that do. Objects implementation of clone allocates space for the new Object using the correct cla

6、ss, without calling any constructors, and then fills in all of the new field values with the old field values. Thus, it is a shallow copy. However, subclasses are permitted to make a deep copy. All array types implement Cloneable, and override this method as follows (it should never fail):public Obj

7、ect clone() try super.clone(); catch (CloneNotSupportedException e) throw new InternalError(e.getMessage(); 2,Java的clone()方法 clone方法将对象复制了一份并返回给调用者。一般而 言,clone()方法满足: 对任何的对象x,都有x.clone() !=x克隆对象与原对象不是同一个对象 对任何的对象x,都有x.clone().getClass()=x.getClass()克隆对象与原对象的类型一样 如果对象x的equals()方法定义恰当,那么 x.clone().equ

8、als(x)应该成立。 Java中对象的克隆 为了获取对象的一份拷贝,我们可以利用Object类的clone() 方法。 在派生类中覆盖基类的clone()方法,并声明为public。 在派生类的clone()方法中,调用super.clone()。 在派生类中实现Cloneable接口。3,Java的类拷贝实现代码class Student implements Cloneable String name;int age;Student(String name,int age)this.name=name;this.age=age;public Object clone()Object o=

9、null;tryo=(Student)super.clone();/Object中的clone()识别出你要复制的是哪一个对象。catch(CloneNotSupportedException e)System.out.println(e.toString();return o; public static void main(String args)Student s1=new Student(“zhangsan”,18);Student s2=(Student)s1.clone();s2.name=“lisi”;s2.age=20;System.out.println(“name=”+s1

10、.name+“,”+“age=”+s1.age);/修改学生2后,不影响学生1的值。为什么我们在派生类中覆盖Object的clone()方法时, 一定要调用super.clone()呢?在运行时刻,Object中的 clone()识别出你要复制的是哪一个对象,然后为此对象 分配空间,并进行对象的复制,将原始对象的内容一一 复制到新对象的存储空间中。 继承自java.lang.Object类的clone()方法是浅复制。 后面的代码可以证明。浅拷贝3,Java的类拷贝实现代码class Professor String name;int age;Professor(String name,int

11、 age)this.name=name;this.age=age; class Student implements Cloneable String name;/常量对象。int age;Professor p;/学生1和学生2的引用值都是一样 的。Student(String name,int age,Professor p)this.name=name;this.age=age;this.p=p;public Object clone()Student o=null;tryo=(Student)super.clone();catch(CloneNotSupportedException

12、e)System.out.println(e.toString();/o.p=(Professor)p.clone();return o; public static void main(String args)Professor p=new Professor(“wanGWu“,50);Student s1=new Student(“zhangsan“,18,p);Student s2=(Student)s1.clone();s2.p.name=“lisi“;s2.p.age=30; System.out.println(“name=“+s1.p.name+“,“+“ag e=“+s1.p.

13、age); /学生1的教授为lisi,age为30,被改了浅拷贝3,Java的类拷贝实现代码class Professor implements Cloneable String name;int age;Professor(String name,int age)this.name=name;this.age=age;public Object clone()Object o=null;tryo=super.clone();catch(CloneNotSupportedException e)System.out.println(e.toString();return o; class St

14、udent implements Cloneable String name;/常量对象。int age;Professor p;/学生1和学生2的引用值都是一样 的。Student(String name,int age,Professor p)this.name=name;this.age=age;this.p=p;public Object clone()Student o=null;tryo=(Student)super.clone();catch(CloneNotSupportedException e)System.out.println(e.toString();o.p=(Pro

15、fessor)p.clone();return o; public static void main(String args)Professor p=new Professor(“wanGWu”,50);Student s1=new Student(“zhangsan”,18,p);Student s2=(Student)s1.clone();s2.p.name=“lisi”;s2.p.age=30; System.out.println(“name=”+s1.p.name+“,”+“ag e=”+s1.p.age);/学生1的教授不改变 深拷贝3,Java的类拷贝实现代码class Prof

16、essor implements Serializable String name;int age;Professor(String name,int age)this.name=name;this.age=age; class Student implements Serializable String name;/常量对象。int age;Professor p;/学生1和学生2的引用值都是一样的。Student(String name,int age,Professor p)this.name=name;this.age=age;this.p=p;public Object fullClone() throws IOException, OptionalDataExceptio

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号