In the book "Java and Patterns" of Dr. Shanhong, this describes the decoration (Decorator) Pattern:
Decorative mode is also known as packaging (Wrapper) mode. Adornment mode extends the functionality of the object transparently to the client, and is an alternative to the inheritance relationship.
Structure of the decoration mode
Decorating mode dynamically attaches more responsibility to an object in a transparent manner to the customer . In other words, the client does not feel that the object is different before and after decorating. Adornment mode expands the functionality of an object without using the creation of more subclasses. (from the network above)
The class diagram for the decorating mode is as follows:
The roles in the decorating mode are:
Abstract component (Context) role: An abstract interface is given to standardize the objects that are ready to receive additional responsibilities.
Concrete Component (CONTEXTIMPL) Role: Defines a class that will receive additional responsibilities.
Decoration (contextwrapper) Role: Holds an instance of a component (Component) object and defines an interface that is consistent with the abstract component interface.
Specific decoration (activity/service/application) role: Responsible for attaching the Component object "affixed" to the responsibility.
< Span style= "font-family: ' ms Shell Dlg '; font-size:18px; Line-height:32px ">contextimpl is the concrete realization of abstract class context, contextwrapper and all its subclass objects hold the context Contextimpl Like. In the context of activity, activity was created in activity Created:
Public final class Activitythread {
... Priva Te 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.activity Info, title, R.parent, R.embeddedid, r.lastnonconfigurationinstances, config);
..... <span style= "White-space:pre" ></span> }
........ }
Private Context createbasecontextforactivity (Activityclientrecord r, & nbsp Final activity activity) { Contextimpl appContext = new Contextimpl (); &NBS P Appcontext.init (R.packageinfo, R.token, this); appcontext.setoutercontext (activity); //For debugging purposes, if the activity's package name contains the value of & nbsp //the "Debug.use-second-display" system property as a substring, then show //its Conte NT on a secondary display if there is one. Context basecontext = appcontext; &NBSP ; String pkgname = Systemproperties.get ("debug.second-display.pkg"); if (pkgname! = null & amp;&!pkgname.isempty () && R.packageinfo.mpackagename.contains (Pkgname)) {&NBsp Displaymanagerglobal DM = displaymanagerglobal.getinstance (); &NBS P for (int displayId:dm.getDisplayIds ()) { if (displa Yid! = display.default_display) { Display display = d M.getrealdisplay (Displayid, R.token); Basecontext = a Ppcontext.createdisplaycontext (display); break;& nbsp } } } return basecontext; }}
The Createbasecontextforactivity () method returns the Contextimpl object byActivity.attach() willthe Contextimpl object is associated with the activity.
Please correct me if there is any wrong place to be continued.
Android and design mode-decorator (Decorator) mode