原型模式:prototype模式

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:12   1009   0

定义:

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.


Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建


浅复制

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


Java的java.lang.Object方法里就提供了克隆方法clone(),原则上似乎所有类都拥有此功能,但其实不然,关于它的使用有如下限制:

1、要实现克隆,必须实现java.lang.Cloneable接口,否则在运行时调用clone()方法,会抛CloneNotSupportedException异常。

2、返回的是Object类型的对象,所以使用时可能需要强制类型转换。

3、该方法是protected的,如果想让外部对象使用它,必须在子类重写该方法,设定其访问范围是public的,参见PackageInfo的clone()方法。

4、Object的clone()方法的复制是采用逐字节的方式从复制内存数据,复制了属性的引用,而属性所指向的对象本身没有被复制,因此所复制的引用指向了相同的对象。由此可见,这种方式拷贝对象是浅拷贝,不是深拷贝。

Java代码 收藏代码
  1. package testPackage;
  2. import java.util.Date;
  3. public class Mokey implements Cloneable {
  4. private int height;
  5. private int weight;
  6. private Date birthDate;
  7. private GoldRingdeStaff goldRingdeStaff;
  8. public Mokey() {
  9. birthDate = new Date();
  10. }
  11. public Date getBirthDate() {
  12. return birthDate;
  13. }
  14. public void setBirthDate(Date birthDate) {
  15. this.birthDate = birthDate;
  16. }
  17. public int getHeight() {
  18. return height;
  19. }
  20. public void setHeight(int height) {
  21. this.height = height;
  22. }
  23. public int getWeight() {
  24. return weight;
  25. }
  26. public void setWeight(int weight) {
  27. this.weight = weight;
  28. }
  29. public Object clone() {
  30. Mokey mokey = null;
  31. try {
  32. mokey = (Mokey) super.clone();
  33. } catch (CloneNotSupportedException e) {
  34. // TODO 自动生成 catch 块
  35. e.printStackTrace();
  36. } finally {
  37. return mokey;
  38. }
  39. }
  40. public GoldRingdeStaff getGoldRingdeStaff() {
  41. return goldRingdeStaff;
  42. }
  43. public void setGoldRingdeStaff(GoldRingdeStaff goldRingdeStaff) {
  44. this.goldRingdeStaff = goldRingdeStaff;
  45. }
  46. }
Java代码 收藏代码
  1. package testPackage;
  2. public class GoldRingdeStaff {
  3. private float height = 100.0f;
  4. private float weight = 10.0f;
  5. public float getHeight() {
  6. return height;
  7. }
  8. public void setHeight(float height) {
  9. this.height = height;
  10. }
  11. public float getWeight() {
  12. return weight;
  13. }
  14. public void setWeight(float weight) {
  15. this.weight = weight;
  16. }
  17. }

Java代码 收藏代码
  1. package testPackage;
  2. public class TestPrototype {
  3. private Mokey mokey= new Mokey();
  4. public void change() {
  5. Mokey copymokey2;
  6. copymokey2 = (Mokey) mokey.clone();
  7. System.out.println("monkey birth date :" + mokey.getBirthDate());
  8. System.out.println("copymokey2 birth date :"+ copymokey2.getBirthDate());
  9. System.out.println("copymokey2 ==monkey :" + (copymokey2 == mokey));
  10. System.out.println("copymokey2 staff ==monkey staff:"
  11. + (copymokey2.getGoldRingdeStaff() == mokey.getGoldRingdeStaff()));
  12. }
  13. public static void main(String[] args) {
  14. TestPrototype t = new TestPrototype();
  15. t.change();
  16. }
  17. }

结果:

monkey birth date :Mon Jul 09 17:25:38 CST 2012

copymokey2 birth date :Mon Jul 09 17:25:38 CST 2012

copymokey2 ==monkey :false

copymokey2 staff ==monkey staff:true

深复制

被复制对象的所有的变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把重复的对象所引用的对象都复制一遍,而这种对被引用到的对象的复制叫做间接复制。


通过上述学习,我们知道Java提供了浅拷贝的方法,那么,如何实现一个深拷贝呢?一般情况下,我们有两种方式来实现:

1. 拷贝对象时,递归地调用属性对象的克隆方法完成。读者可以根据具体的类,撰写出实现特定类型的深拷贝方法。


一般地,我们很难实现一个一般性的方法来完成任何类型对象的深拷贝。有人根据反射得到属性的类型,然后依照它的类型构造对象,但前提是,这些属性的类型必须含有一个公有的默认构造方法,否则作为一个一般性的方法,很难确定传递给非默认构造方法的参数值;此外,如果属性类型是接口或者抽象类型,必须提供查找到相关的具体类方法,作为一个一般性的方法,这个也很难办到。

2. 如果类实现了java.io.Serializable接口,把原型对象序列化,然后反序列化后得到的对象,其实就是一个新的深拷贝对象。


Java代码 收藏代码
  1. public Object deepClone() throws IOException, ClassNotFoundException {
  2. ByteArrayOutputStream bo = new ByteArrayOutputStream();
  3. ObjectOutputStream oo= new ObjectOutputStream(bo);
  4. oo.writeObject(this);
  5. ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  6. ObjectInputStream oi= new ObjectInputStream(bi);
  7. return (oi.readObject());
  8. }
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP