Android design mode-prototype and android Design Mode
1. Definition:
Use a prototype instance to specify the object type and copy the prototype to create a new object.
2. Purpose:
Create another custom object from an object without any details.
3. functions:
3.1. simplified object creation;
3.2. The performance of processing large objects is much higher than that of new objects.
4. Category:
4.1 shallow copy: the basic data type in the object to be copied. It is not copied for arrays, container objects, and reference objects.
4.2 deep copy: All types are copied.
5. Note:
5.1 when an Object implements the Cloneable interface, the Object clone () method must be changed to public;
5.2 for basic data types, the encapsulation type of String does not need to be processed. They all perform deep copies.
6. Simple demo:
Shallow copy:
Package com. example. demo. prototype;/*** shallow copy * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public class Prototype implements Cloneable {private int num; private String name; public int getNum () {return num;} public void setNum (int num) {this. num = num;} public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Overridepublic Object clone () {Prototype prototype = null; try {prototype = (Prototype) super. clone ();} catch (CloneNotSupportedException e) {e. printStackTrace ();} return prototype ;}}
Deep copy:
Package com. example. demo. prototype; import java. util. arrayList; import java. util. vector;/*** deep copy * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public class DeepPrototype implements Cloneable {private String name; private ArrayList <String> arrayList; private DeepObject deepObject; public String getName () {return name;} public void setName (String name) {this. name = name;} public ArrayList <String> getArrayList () {return arrayList;} public void setArrayList (ArrayList <String> arrayList) {this. arrayList = arrayList;} public DeepObject getDeepObject () {return deepObject;} public void setDeepObject (DeepObject deepObject) {this. deepObject = deepObject;}/*** clone method */@ SuppressWarnings ("unchecked") @ Overridepublic Object clone () {DeepPrototype prototype = null; try {prototype = (DeepPrototype) super. clone (); prototype. arrayList = (ArrayList <String>) this. arrayList. clone (); prototype. deepObject = (DeepObject) this. deepObject. clone ();} catch (CloneNotSupportedException e) {e. printStackTrace ();} return prototype;} class DeepObject implements Cloneable {String name; protected Object clone () {DeepObject deep = null; try {deep = (DeepObject) super. clone ();} catch (CloneNotSupportedException e) {e. printStackTrace () ;}return deep ;};}}
7. Application of prototype in Android:
The most obvious example is Intent, but it seems that its usage is unknown.
However, it is still a new object.
public class Intent implements Parcelable, Cloneable { /** * 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; 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); } } @Override public Object clone() { return new Intent(this); }}