Android Design Pattern series (10)-SDK source code prototype

Source: Internet
Author: User

The CV family should easily understand the principles of the prototype, copy and paste it, and check whether the actual situation is modified. In fact, this is the prototype.
From the Java perspective, the prototype mode has an obvious feature, namely implementing the cloneable clone () method.
The prototype mode allows you to quickly clone another new object that is similar to an existing object.

1. Intention
Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
Popular Words:Clone Deep copy Shortest copy

2. structure chart andCode
Its structure is very simple. We use intent as an example:


The clone method of intent is very simple:

 
@ Override public object clone () {return new intent (this );}

Returns a new intent object.
The clone operation is divided into deep copy and light copy. The light copy statement simply assigns all values and references of the original object to the new object. Deep copy not only assigns the value of the original object to the new object, but also re-creates the referenced object of the original object and then assigns it to the new object.
Let's analyze whether intent is a light copy or a deep copy:

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; // The following are the reference objects that have been re-created, that is, the IF (O. mcategories! = NULL) {This. mcategories = new hashset <string> (O. mcategories);} If (O. mextras! = NULL) {This. Mexico tras = new bundle (O. Mexico TRAS);} If (O. msourcebounds! = NULL) {This. msourcebounds = new rect (O. msourcebounds );}}

Here, the reason why intent needs to override the object clone method is related to deep copy.
In fact, we can view the source code and comments of the clone () method of the object. The default Super. Clone () method uses the shortest copy:

/*** creates and returns a copy of this {@ code object }. the default * Implementation returns a so-called "Shallow" Copy: it creates a new * instance of the same class and then copies the field values (including * object references) from this instance to the new instance. A "deep" Copy, * In contrast, wocould also recursively clone nested objects. A subclass that * Needs to implement this kind of cloning shocould call {@ code super. clone ()} * to create the new instance and then create deep copies of the nested, * mutable objects. */protected object clone () throws clonenotsupportedexception {If (! (This instanceof cloneable) {Throw new clonenotsupportedexception ("class doesn't implement cloneable");} return internalclone (cloneable) This);} 

This form is a simple prototype. If the number of prototypes to be created is not fixed, you can create a prototype manager. before copying a prototype object, the client first checks the prototype in the prototype manager.
Whether a prototype object that meets the conditions exists. If yes, use it directly. If not, clone a prototype, which is called the registration form.
The applicable prototype can hide the specific class of the product to the customer, thus reducing the number of names that the customer knows. In addition, the customer does not need to change the name.
The defect of the prototype mode is that every prototype subclass must implement the cloneable interface, which is sometimes difficult to implement.

3. Results
(1). Creation Mode
(2) add and delete products at runtime
(3). Change only to specify the new object (CTRL + V, and then modify)
(4). Change the structure to specify a new object. (Similar to 2, implementation is different)
(5). Reduce the subclass Construction

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.