Android design mode-decoration Mode

Source: Internet
Author: User

Android design mode-decoration Mode

1. Definition:

Attach additional responsibilities to an object dynamically keeping the same interface.
Decoators provide a flexible alternative to subclassing for extending functionality.

Dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object.



2. In the decoration mode, the essence is to expand and use setComponent () to encapsulate objects without changing the original code structure,
In this way, how to use this object is isolated from the specific implementation of the Object. Each decoration object only cares about its own functions and does not need to care about how to add it to this object chain.


3. dynamically add more functions to existing functions


4. dynamically add some additional responsibilities to the object;


5. more flexible than extended inheritance and implementation!


6. Separate the core functions from the decoration functions!

 

 

In general, we use the following methods:

 

Package com. example. demo. decorator;/*** decoration mode * Common class * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public abstract class UserInfo {public abstract String getName ();}

Package com. example. demo. decorator;/*** common implementation * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public class UserInfoImp extends UserInfo {@ Overridepublic String getName () {return "UserInfoImp ";}}

Now we are expanding;

 

 

Package com. example. demo. decorator;/*** without changing the parent class, * corresponding expansion * @ author qubian * @ data June 3, 2015 * @ e-mail naibbian@163.com **/public abstract class Decorator extends UserInfo {private UserInfo pattern; public void SetComponent (UserInfo p) {pattern = p ;}@ Overridepublic String getName () {StringBuilder name = new StringBuilder (); if (pattern! = Null) {name. append (pattern. getName ();} return name. toString ();}}

Implementation of expansion:
Package com. example. demo. decorator;/*** extended implementation class * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public class DecoratorImp extends Decorator {@ Overridepublic String getName () {StringBuilder sb = new StringBuilder (); sb. append (super. getName (); sb. append ("DecoratorImp"); return sb. toString ();}}

 

Specific use:

 

Package com. example. demo. decorator; import android. util. log;/*** use * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public class UseDecorator {public static String TAG = "UseDecorator"; public void toUserDecorator () {// use UserInfo dp = new UserInfoImp (); Log. I (TAG, dp. getName (); // Decorator mode is used in the following cases // 1. you need to expand the functions of a class or add additional responsibilities to a class. // 2. You need to dynamically add functions to an object. These functions can be dynamically revoked. // 3. A large number of functions are generated by the arrangement and combination of some basic functions to make the inheritance relationship unrealistic. // 4. When the subclass generation method cannot be used for expansion. // In one case, there may be a large number of independent extensions. To support each combination, a large number of subclasses will be generated, resulting in explosive growth of the number of subclasses. // Another possible cause is that the class definition is hidden, or the class definition cannot be used to generate a subclass. DecoratorImp d = new DecoratorImp (); d. SetComponent (dp); Log. I (TAG, d. getName ());}}

In the Android framework, the decoration mode is also widely used;

I have referenced some online materials;

 

1. Service Application activities are inherited from ContextWrapper, while ContextWrapper is actually a decoration of Context;

2. The WindowDecorator is the decoration of the Window. However, there is no research on it for the time being. Write it first and study it later;

 

 

public class ContextWrapper extends Context {    Context mBase;    public ContextWrapper(Context base) {        mBase = base;    }        /**     * Set the base context for this ContextWrapper.  All calls will then be     * 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 IllegalStateException("Base context already set");        }        mBase = base;    }    /**     * @return the base context as set by the constructor or setBaseContext     */    public Context getBaseContext() {        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);    }      }


 

 

 

 

 

 

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.