Android design mode series-prototype mode

Source: Internet
Author: User

CV A family, it should be easy to understand the prototype model of the principle, copy, after pasting to see if the specific situation is modified, in fact, this is the prototype model.
From the Java point of view, the general use of prototype mode has an obvious feature is to implement the Cloneable clone () method.
Prototype mode to quickly clone a new object that is similar to the one that already exists.

1. Intentions
Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.
Hot words: clone deep copy shallow copy

2. Structure diagram and code
Its structure diagram is very simple, we take intent as an example:


The Clone method of intent is very simple:

    1. @Override
    2. Public Object Clone () {
    3. return New Intent (this);
    4. }

Returns a new intent object.
Cloning operation of deep copy and shallow copy, shallow copy is the original object all the values and references directly to the new object. Deep copy not only assigns the value of the original object to the new object, but also re-creates the reference object of the original object again and assigns it to the new object.
Let's analyze whether the intent is a shallow copy or a deep copy:

  1. Public Intent (Intent o) {
  2. this.maction = o.maction;
  3. this.mdata = O.mdata;
  4. this.mtype = O.mtype;
  5. this.mpackage = o.mpackage;
  6. this.mcomponent = o.mcomponent;
  7. this.mflags = o.mflags;
  8. ///The following is a reference object that has been recreated, is a deep copy
  9. if (o.mcategories! = null) {
  10. this.mcategories = new hashset<string> (o.mcategories);
  11. }
  12. if (o.mextras! = null) {
  13. This.mextras = new Bundle (O.mextras);
  14. }
  15. if (o.msourcebounds! = null) {
  16. this.msourcebounds = new Rect (o.msourcebounds);
  17. }
  18. }

Here's why we intent to rewrite the Clone method of object, which is related to the deep copy.
In fact, we look at the Clone () method of object source code and comments, the default Super.clone () is a shallow copy:

  1. /**
  2. * Creates and returns a copy of this {@code Object}. The default
  3. * Implementation returns a so-called "shallow" copy:it creates a new
  4. * Instance of the same class and then copies the field values (including
  5. * Object references) from this instance to the new instance. A "Deep" copy,
  6. * In contrast, would also recursively clone nested objects. A Subclass that
  7. * Needs to implement the kind of cloning should call {@code super.clone ()}
  8. * To create the new instance and then create a deep copies of the nested,
  9. * mutable objects.
  10. */
  11. Protected Object Clone () throws clonenotsupportedexception {
  12. if (! ( this instanceof cloneable)) {  
  13. throw New Clonenotsupportedexception ("Class doesn ' t implement cloneable");
  14. }
  15. return Internalclone ((cloneable) this );
  16. }

This form is a prototype pattern in a simple form, and if the number of prototypes that need to be created is not fixed, you can create a prototype manager that the client will view in the prototype manager before copying the prototype object.
If there is a prototype object that satisfies the condition, if there is one, use it directly, if not, clone a prototype pattern called the registration form.
A prototype model can be used to hide a specific class of products from the customer, thus reducing the number of names the customer knows, and the customer does not need to change
The flaw in prototype mode is that the subclass of each prototype must implement the Cloneable interface, which is sometimes difficult to implement.

3. Effects
(1). Create pattern
(2). Add and remove products at run time
(3). Change only to specify new objects (Ctrl + V, then modify)
(4). Change the structure to specify a new object. (Similar to 2, implementation is different)
(5). Reduce the construction of sub-classes

Android design mode series-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.