Android and design pattern-Decorator pattern

Source: Internet
Author: User

Android and design pattern-Decorator pattern

The Decorator mode is described as follows in Dr. Yan Hong's book "JAVA and patterns:
  Decoration mode, also known as Wrapper mode. The decoration mode extends the object functions in a transparent way to the client. It is an alternative to the inheritance relationship.
Decoration mode structure

Decoration modeDynamically attaches more responsibilities to an object in a transparent manner to the customer. In other words, the client does not think that objects are different before and after decoration. The decoration mode can extend the functions of objects without creating more child classes. (The above is from the Network)

The decoration mode class diagram is as follows:

Roles in the decoration mode include:
● Abstract component (Context) Role: provides an abstract interface to standardize the objects for receiving additional responsibilities.
● ContextImpl role: defines a class that will receive additional responsibilities.
● ContextWrapper: holds an instance of a Component object and defines an interface consistent with the abstract Component Interface.
● Specific Decoration (Activity/Service/Application) Role: "attaches" an additional responsibility to the component object.

 

ContextImpl is the concrete implementation of the Context abstract class. ContextWrapper and the Context held by all its subclass objects are ContextImpl objects. Take the Context of the Activity as an example. The Activity is created in ActivityThread. When the Activity is created:

 

public final class ActivityThread {
     .......     private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {          ......            if (activity != null) {                Context appContext = createBaseContextForActivity(r, activity);                CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());                Configuration config = new Configuration(mCompatConfiguration);                if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "                        + r.activityInfo.name + " with config " + config);                activity.attach(appContext, this, getInstrumentation(), r.token,                        r.ident, app, r.intent, r.activityInfo, title, r.parent,                        r.embeddedID, r.lastNonConfigurationInstances, config);
              .......    }
            ........    }
    private Context createBaseContextForActivity(ActivityClientRecord r,            final Activity activity) {        ContextImpl appContext = new ContextImpl();        appContext.init(r.packageInfo, r.token, this);        appContext.setOuterContext(activity);        // For debugging purposes, if the activity's package name contains the value of        // the "debug.use-second-display" system property as a substring, then show        // its content on a secondary display if there is one.        Context baseContext = appContext;        String pkgName = SystemProperties.get("debug.second-display.pkg");        if (pkgName != null && !pkgName.isEmpty()                && r.packageInfo.mPackageName.contains(pkgName)) {            DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();            for (int displayId : dm.getDisplayIds()) {                if (displayId != Display.DEFAULT_DISPLAY) {                    Display display = dm.getRealDisplay(displayId, r.token);                    baseContext = appContext.createDisplayContext(display);                    break;                }            }        }        return baseContext;    }}

After the createBaseContextForActivity () method returns the ContextImpl object, it associates the ContextImpl object with the activity through Activity. attach.

 

 

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.