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.