[Design mode] prototype)

Source: Internet
Author: User

Abstract:

1. This article will introduce in detail the principles of the prototype mode and the application of the actual code, especially the Android system code.

Outline:

1. Introduce the prototype

2. Concepts and advantages and disadvantages of Prototype

3. Use of the prototype for copying

4. Application of prototype in Android source code

1. Start with a field:

Gg and mm often chat on QQ, but Gg's typing speed is slow, such as snail crawling. Every time Mm completes restoration or greetings in an instant, GG will try its best to quickly type, even so, it still makes MM a little unhappy. MM said that the reply information is so slow, obviously it is not dedicated, don't care about her. Ah, GG is also hard to argue, but there is indeed no way.

One day, GG wanted his friend K to talk about his difficulties. Kton laughed. Said: "silly, why don't you go to the Internet to collect some nasty words and theme related to your frequent conversations? Copy these things and save them in your computer or USB flash drive, in this way, you can borrow it for the next chat! ", "K is K. Why didn't I think of it ~ Wonderful ~ Wonderful! _ ^ "," but don't be so happy. These things should be modified as appropriate. Otherwise, if you make a wrong name, just wait for your mm to throw you to death ~" K added, "Well, that's right. Thank you, Brother K, for solving my problem." GG said happily.

This is where mm is used to chat with GG on the Internet. GG is dedicated to copying the prepared words and sending them to mm after slight modification. mm is almost beautiful... (From Android's big talk Design Model)

2. Introduction to the prototype mode 2.1 What is the prototype?

When Class A creates an instance, it performsCloneTo quickly obtain the initialization data consistent with the input object. Cloning is the most prominent feature of the prototype.

Class A is called an isolation class, because the prototype isolates Class A without the change of Class B.

Class B is called a prototype class and implements the clone () interface to clone Class. Class B is easy to change, but the Implemented interfaces are not easy to change. Class B is usually an abstract class. The instance passed to Class A is an instance of the subclass of Class B.

2.2 What are the benefits of doing so?

1. Convenient. If a prototype object contains a large amount of data, it is unrealistic to assign values one by one. By cloning, we can quickly obtain all the data of this prototype object.

2. Low coupling. If the prototype object is a "variable class", that is, this class may have various inheritance types, or this class may be constantly modified as it is developed. However, as long as the interfaces implemented by Class B are stable and not easy to change, the prototype mode can enable the isolation class to maintain the stability of the interface and does not need to change with the changes of the class.

3.1 copy:

There are two types of Clones: shallow copy and deep copy. A shortest copy is a clone operation. It directly assigns values to all attributes of the cloned class, instead of recursive cloning. Recursive cloning is to call its own cloning method for its own attributes. For the basic types (seven basic types: Boolean, Char, byte, short, float, double. Long), assign a value to a copy of the basic type. If it is a non-basic type, the referenced copy is obtained. What does referenced copy mean? This means that, after direct assignment, the instances of the two common classes point to the same data space. That is to say, their modifications will affect each other. Let's take an example:

Two Classes A and B have the following attributes:

Class A {    int a1;    String a2;    B b;}Class B {    int b1;    String b2;}

If we want to implement the shortest copy of Class A, its clone method will be like this:

Object clone() {    return super.clone();}

The clone method has been implemented for the object of the class, and we only need to call it. Here object. Clone () is actually equivalent:

A aa = new A();aa.a1 = this.a1;aa.a2 = this.a2;aa.b = this.b;return a;

Of course, this is just equivalent, not a real implementation method. After all, the object does not know what attributes this class A has. The king of the ten categories is implemented through byte copy, and the Native method is called. Class B is a non-basic type. Therefore, if you assign a value directly, only the copies referenced by B are obtained. The newly cloned AA instance and original Instance a point to the data space of the same B instance. Therefore, the changes to A. B will affect the changes to B of the original Instance.

For deep copy, we should implement the following:

Object clone() {    A aa = (A)super.clone();    aa.b = (B)a.b.clone();    return (Object)aa;}

In fact, it is enough to clone a non-basic type attribute again. If there are still non-basic types in this attribute, deep copy is also required. This exposes the prototype.Disadvantages: The class attributes that implement the prototype mode must all implement clone (). Otherwise, this mode cannot be implemented. It may not be difficult for a newly created class, but if it is added in the middle and late stages, it will require a lot of changes.

Because Java intentionally concealed the pointer concept, the understanding here is a bit complicated. From the perspective of C language, Int, float and other types are basic types, and they own their own storage space when declaring them, when the value is assigned, the value is copied again. In our own struct, we declare the corresponding struct * pointer. Different pointers direct to the same memory block, and their modifications will affect each other.

3.2 interger , String And other packaging issues:

Careful students may find that string is not the basic type. Why is it possible to assign a value directly on it? String storage is actually implemented through char []. Similarly, interger is an int packaging class. One of the characteristics of the packaging class is that the operation on its value will reflect the nature of its corresponding basic type. For details about this issue, visit http://freej.blog.51cto.com/235241/168676.

3.3 What kind of copy is used in the prototype mode?

The design mode does not specify which copy is used for the prototype mode. We should select deep copy and light copy based on the actual situation. Of course, there are many scenarios for deep copy applications. In addition, the copy operation does not specify all attributes to be copied. You can select the attributes based on the actual situation.

4. Android Examples of source code practice:

Example: Intent

Intent is a class that implements a simple prototype mode. Its clone is as follows:

@Overridepublic Object clone() {    return new Intent(this);}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 HashSet<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);    }}

Therefore, intent is a deep copy. Interestingly, its properties are also copied using the constructor. In this case, both the "isolation class" and "variable class" mentioned above are themselves.

The new intent (intent O) constructor is not uncommon in the source code. For example, launchermodel. Java in launcher3, launcheractivity. Java in frameworks, and dockeventreceiver in settings. You can search for "intent" or "intent (mintent.

Copyright. For more information, see the source:

Http://www.cnblogs.com/sickworm/p/4016081.html

[Design mode] prototype)

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.