<span id="Label3"></p><p><p><span style="font-size:14px">1. definition:</span></p></p><p><p><span style="font-size:14px">Attach additional responsibilities to an object dynamically keeping the same interface.<br>Decoators provide a flexible alternative to subclassing for extending Functionality.</span></p></p><p><p><span style="font-size:14px">The ability to dynamically extend an object without having to change the original class file and use Inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.<br></span></p></p><p><p><span style="font-size:14px"><br><br>2, Decorative mode, The essence is to expand, do not change the original code structure, the use of SetComponent () object encapsulation,<br>How to use this object is isolated from the specific implementation of the object, and each decorator only cares about its own functionality, and does not need to be concerned with how to add it to the object Chain.</span></p></p><p><p><span style="font-size:14px"><br>3, for the existing function dynamic add more multi-functional way</span></p></p><p><p><span style="font-size:14px"><br>4, dynamic to add some additional responsibilities to the object;</span></p></p><p><p><span style="font-size:14px"><br>5, more flexible than the expansion of inheritance and implementation!</span></p></p><p><p><span style="font-size:14px"><br>6, the core functions and decorative functions separate!<br></span></p></p><p><p><br></p></p><p><p><br></p></p><p><p><span style="font-size:14px">In general, our common wording:</span></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">Package com.example.demo.decorator;/** * Decoration mode * General class * @author Qubian * @data June 3, 2015 * @email [email protected] * */PUBL IC abstract class UserInfo {public abstract String getName ();}</pre></pre><br><pre name="code" class="java"><pre name="code" class="java">Package com.example.demo.decorator;/** * General Implementation * @author Qubian * @data June 3, 2015 * @email [email protected] * */public cl Userinfoimp extends Userinfo{@Overridepublic String getName () {return "userinfoimp";}}</pre></pre><br>Now began to expand;<p><p></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">Package com.example.demo.decorator;/** * Without changing the parent class, * to expand accordingly * @author Qubian * @data June 3, 2015 * @email [email PROTECTE d] * */public abstract class Decorator extends Userinfo{private UserInfo pattern;public void setcomponent (UserInfo P) {patt Ern = p;} @Overridepublic String getName () {StringBuilder name= new StringBuilder (); if (pattern!=null) {name.append ( Pattern.getname ());} return name.tostring ();}}</pre></pre><br>The implementation of Expansion:<pre code_snippet_id="682636" snippet_file_name="blog_20150603_4_6709442" name="code" class="java"><pre code_snippet_id="682636" snippet_file_name="blog_20150603_4_6709442" name="code" class="java">Package com.example.demo.decorator;/** * Extended Implementation class * @author Qubian * @data June 3, 2015 * @email [email protected] * */public cl Decoratorimp extends Decorator{@Overridepublic String getName () {StringBuilder sb = new StringBuilder (); sb.append ( Super.getname ()); sb.append ("decoratorimp"); return sb.tostring ();}}</pre></pre><br><p><p></p></p><p><p><span style="font-size:14px">Specific use:</span></p></p><p><p><span style="font-size:14px"></span></p></p><pre code_snippet_id="682636" snippet_file_name="blog_20150603_5_4729503" name="code" class="java"><pre code_snippet_id="682636" snippet_file_name="blog_20150603_5_4729503" name="code" class="java">Package Com.example.demo.decorator;import android.util.log;/** * use * @author Qubian * @data June 3, 2015 * @email [email Pro tected] * */public class usedecorator {public static String tag= "usedecorator";p ublic void Touserdecorator () {// Normal use of userinfo DP = new Userinfoimp (); LOG.I (TAG, dp.getname ());//the following conditions use decorator mode//1. You need to extend the functionality of a class or add additional responsibilities to a class. 2. It is necessary to dynamically add functionality to an object, which can then be withdrawn dynamically. 3. It is necessary to increase the number of functions produced by the permutation of some basic functions, thus making the inheritance relationship Impractical. 4. When a method of generating subclasses cannot be used for Expansion. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of Subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate Subclasses. Decoratorimp d = new Decoratorimp ();d. setcomponent (dp); LOG.I (TAG, D.getname ());}}</pre></pre><br><p><p><span style="font-size:14px">In the Android framework, the decorative mode is also widely used;</span></p></p><p><p><span style="font-size:14px">But I don't really have to study it very specifically.</span></p></p><p><p><span style="font-size:14px">Reference to some information on the internet;</span></p></p><p><p><span style="font-size:14px">which has said that</span></p></p><p><p><span style="font-size:14px">1, for <span style="font-family:Monaco">Service </span> <span style="font-family:Monaco">application Activity are inherited from </span> <span style="font-family:Monaco">contextwrapper, and </span> <span style="font-family:Monaco">contextwrapper is actually <span style="font-family:Monaco">Contex The decoration of t;</span> </span></span></p></p><p><p><span style="font-size:14px"><span style="font-family:Monaco"><span style="font-family:Monaco">2, is the windowdecorator is the window decoration, but, for the time being no corresponding research, first written on, later study;</span></span></span></p></p><p><p><span style="font-size:14px"><span style="font-family:Monaco"><span style="font-family:Monaco"><br></span></span></span></p></p><p><p> <span style="font-size:14px"><span style="font-family:monaco"><span style="font-family:monaco"> </span> </span> </span> /p></p></p><pre code_snippet_id="682636" snippet_file_name="blog_20150603_6_1255065" name="code" class="java">public class Contextwrapper extends context {context mbase; Public Contextwrapper (Context Base) {mbase = base; }/** * Set The base context for this contextwrapper. All calls'll then being * delegated to the base Context. Throws * illegalstateexception If a base context has already been set. * * @param base The new base context for this wrapper. */protected void Attachbasecontext (Context base) {if (mbase! = null) {throw new illegalstateexcept Ion ("Base Context already set"); } mbase = base; }/** * @return The base context as set by the constructor or Setbasecontext */public context Getbasecontex T () {return mbase; } @Override public Assetmanager getassets () {return mbase.getassets (); } @Override public Resources getresources () {return mbase.getresources (); } @Override public Packagemanager getpackagemanager () {return mBaSe.getpackagemanager (); } @Override public contentresolver getcontentresolver () {return mbase.getcontentresolver (); } @Override public Looper getmainlooper () {return mbase.getmainlooper (); } @Override public Context getapplicationcontext () {return mbase.getapplicationcontext (); } @Override public void SetTheme (int resid) {mbase.settheme (resid); } }</pre><br><br><p><p></p></p><p><p><span style="font-family:Monaco"><span style="font-size:11px"><br></span></span><span style="font-size:14px"></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Monaco"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Monaco"><br></p></p><p><p><br></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p>Android Design mode-decorative mode</p></p></span>
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