Android design mode-prototype mode

Source: Internet
Author: User

1. Definition:

Specify the kind of objects created with the prototype instance and create new objects by copying the prototypes.

2. Purpose:

Create another customizable object from one object without needing to know any of the creation details.

3. Function:

3.1, simplify the creation of objects;

3.2, for the processing of large objects, the performance is much higher than the new.

4. Classification:

4.1 Shallow copy: copies the basic data types in the object, and does not copy for arrays, container objects, reference objects, and so on.

4.2 Deep Copy: Copies all types.

5. Note:

5.1 Object Implementations cloneable interface, you must set the The Object Clone () method changes to public ;

5.2 For the base data type, the package type,String does not need to be processed. All they do is deep copy.


6, the Simple demo:

Shallow copy:

Package com.example.demo.prototype;/** *  Light copy * @author Qubian * @data June 4, 2015 * @email [EMAIL protected] * */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 (Clonenotsup Portedexception 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 [email protected] * */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 {Protot ype = (deepprototype) super.clone ();p rototype.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, the use of prototype mode in Android:

The most obvious example is intent, but it seems that it is not yet known for its usefulness.

But look closely, actually still is the object of new.

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); }}


Android design mode-prototype mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.