标签:
原型模式是一种创建型设计模式,它通过复制一个已经存在的实例来返回新的实例,而不是新建实例.被复制的实例就是我们所称的原型,这个原型是可定制的。
原型模式多用于创建复杂的或者耗时的实例, 因为这种情况下,复制一个已经存在的实例可以使程序运行更高效,或者创建值相等,只是命名不一样的同类数据。深拷贝: 对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制。
⑵Java中对象的克隆
①为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。
②在派生类中覆盖基类的clone()方法,并声明为public。
③在派生类的clone()方法中,调用super.clone()。
④在派生类中实现Cloneable接口。
package com.designpattern.bean; /** * 机器人实例 * * @author chao * */ public class Robot { public String name;// 名字 public int age;// 寿命 public Battery battery;// 电池 @Override public String toString() { return " Robot hashCode:" + hashCode() + " name:" + name + " age:" + age + (battery == null ? "" : battery.toString()); } /** * 电池实例 * * @author chao * */ public static class Battery { public String name;// 电池名称 public int level;// 电池电量 0-100 @Override public String toString() { return " Battery hashCode:" + hashCode() + " name:" + name + " level:" + level; } } }
package com.designpattern.prototype; import com.designpattern.bean.Robot; /** * 浅克隆 * * @author chao * */ public class ShallowClone implements Cloneable { public Robot robot; public String name; public int number; @Override public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { System.out.println(e.getMessage()); } return null; } @Override public String toString() { return " ShallowClone hashCode:" + hashCode() + " name:" + name + " number:" + number + (robot == null ? "" : robot.toString()); } }测试
package com.designpattern.prototype; import com.designpattern.bean.Robot; import com.designpattern.bean.Robot.Battery; public class CloneTest { public static void main(String[] args) { testShallowClone(); } private static void testShallowClone() { ShallowClone shallowClone = new ShallowClone(); shallowClone.name = "1"; shallowClone.number = 1; shallowClone.robot = new Robot(); shallowClone.robot.name = "1"; shallowClone.robot.battery = new Battery(); shallowClone.robot.battery.name = "1"; System.out.println(shallowClone.toString()); ShallowClone shallowClone2 = (ShallowClone) shallowClone.clone(); System.out.println(shallowClone2.toString()); shallowClone.name = "2"; shallowClone.number = 2; shallowClone.robot.name = "2"; shallowClone.robot.battery.name = "2"; System.out.println(shallowClone.toString()); System.out.println(shallowClone2.toString()); } }
ShallowClone hashCode:1704856573 name:1 number:1 Robot hashCode:705927765 name:1 age:0 Battery hashCode:366712642 name:1 level:0 ShallowClone hashCode:1829164700 name:1 number:1 Robot hashCode:705927765 name:1 age:0 Battery hashCode:366712642 name:1 level:0 ShallowClone hashCode:1704856573 name:2 number:2 Robot hashCode:705927765 name:2 age:0 Battery hashCode:366712642 name:2 level:0 ShallowClone hashCode:1829164700 name:1 number:1 Robot hashCode:705927765 name:2 age:0 Battery hashCode:366712642 name:2 level:0
package com.designpattern.prototype; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import com.designpattern.bean.Robot; /** * 深复制 * * @author chao * */ public class DeepClone implements Serializable { public Robot robot; public String name; public int number; public Object deepclone() { // 将对象写到流里 ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo; try { oo = new ObjectOutputStream(bo); oo.writeObject(this); // 从流里读出来 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi = new ObjectInputStream(bi); return (oi.readObject()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override public String toString() { return " ShallowClone hashCode:" + hashCode() + " name:" + name + " number:" + number + (robot == null ? "" : robot.toString()); } }
package com.designpattern.prototype; import com.designpattern.bean.Robot; import com.designpattern.bean.Robot.Battery; public class CloneTest { public static void main(String[] args) { testDeepClone(); } private static void testDeepClone() { DeepClone deepClone = new DeepClone(); deepClone.name = "1"; deepClone.number = 1; deepClone.robot = new Robot(); deepClone.robot.name = "1"; deepClone.robot.battery = new Battery(); deepClone.robot.battery.name = "1"; System.out.println(deepClone.toString()); DeepClone deepclone2 = (DeepClone) deepClone.deepclone(); System.out.println(deepclone2.toString()); deepClone.name = "2"; deepClone.number = 2; deepClone.robot.name = "2"; deepClone.robot.battery.name = "2"; System.out.println(deepClone.toString()); System.out.println(deepclone2.toString()); } } }
ShallowClone hashCode:1704856573 name:1 number:1 Robot hashCode:705927765 name:1 age:0 Battery hashCode:366712642 name:1 level:0 ShallowClone hashCode:1735600054 name:1 number:1 Robot hashCode:21685669 name:1 age:0 Battery hashCode:2133927002 name:1 level:0 ShallowClone hashCode:1704856573 name:2 number:2 Robot hashCode:705927765 name:2 age:0 Battery hashCode:366712642 name:2 level:0 ShallowClone hashCode:1735600054 name:1 number:1 Robot hashCode:21685669 name:1 age:0 Battery hashCode:2133927002 name:1 level:0
public class Intent implements android.os.Parcelable, Cloneable { @Override public Object clone() { return new Intent(this); } /** * Copy constructor. */ public Intent(Intent o) { this.mAction = o.mAction; this.mData = o.mData; this.mType = o.mType; this.mPackage = o.mPackage; this.mComponent = o.mComponent; this.mFlags = o.mFlags; this.mContentUserHint = o.mContentUserHint; if (o.mCategories != null) { this.mCategories = new ArraySet<String>(o.mCategories); } if (o.mExtras != null) { this.mExtras = new Bundle(o.mExtras); } if (o.mSourceBounds != null) { this.mSourceBounds = new Rect(o.mSourceBounds); } if (o.mSelector != null) { this.mSelector = new Intent(o.mSelector); } if (o.mClipData != null) { this.mClipData = new ClipData(o.mClipData); } } .......... }
标签:
原文地址:http://blog.csdn.net/robertcpp/article/details/51607301