Android源码学习之装饰模式应用
2012-12-31 01:04
567 查看
装饰模式定义: |
Attach additional responsibilities to an object dynamically keeping the same interface. Decoators provide a flexible alternative to subclassing for extending functionality. |
动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。 |
public class Activity extends ContextThemeWrapper implements LayoutInflater.Factory2, Window.Callback, KeyEvent.Callback, OnCreateContextMenuListener, ComponentCallbacks2 { private static final String TAG = "Activity"; /** Standard activity result: operation canceled. */ public static final int RESULT_CANCELED = 0; /** Standard activity result: operation succeeded. */ public static final int RESULT_OK = -1; /** Start of user-defined activity results. */ public static final int RESULT_FIRST_USER = 1; private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState"; private static final String FRAGMENTS_TAG = "android:fragments"; private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds"; private static final String SAVED_DIALOGS_TAG = "android:savedDialogs"; private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_"; private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_"; private static class ManagedDialog { Dialog mDialog; Bundle mArgs; } private SparseArray<ManagedDialog> mManagedDialogs; // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called. private Instrumentation mInstrumentation; private IBinder mToken; private int mIdent; /*package*/ String mEmbeddedID; private Application mApplication; /*package*/ Intent mIntent; private ComponentName mComponent; /*package*/ ActivityInfo mActivityInfo; /*package*/ ActivityThread mMainThread; Activity mParent; boolean mCalled; boolean mCheckedForLoaderManager; boolean mLoadersStarted; /*package*/ boolean mResumed; private boolean mStopped; boolean mFinished; boolean mStartedActivity; /** true if the activity is going through a transient pause */ /*package*/ boolean mTemporaryPause = false; /** true if the activity is being destroyed in order to recreate it with a new configuration */ /*package*/ boolean mChangingConfigurations = false; /*package*/ int mConfigChangeFlags; /*package*/ Configuration mCurrentConfig; private SearchManager mSearchManager; private MenuInflater mMenuInflater; static final class NonConfigurationInstances { Object activity; HashMap<String, Object> children; ArrayList<Fragment> fragments; SparseArray<LoaderManagerImpl> loaders; } /* package */ NonConfigurationInstances mLastNonConfigurationInstances; private Window mWindow; private WindowManager mWindowManager; /*package*/ View mDecor = null; /*package*/ boolean mWindowAdded = false; /*package*/ boolean mVisibleFromServer = false; /*package*/ boolean mVisibleFromClient = true; /*package*/ ActionBarImpl mActionBar = null; private CharSequence mTitle; private int mTitleColor = 0; final FragmentManagerImpl mFragments = new FragmentManagerImpl(); SparseArray<LoaderManagerImpl> mAllLoaderManagers; LoaderManagerImpl mLoaderManager; private static final class ManagedCursor { ManagedCursor(Cursor cursor) { mCursor = cursor; mReleased = false; mUpdated = false; } private final Cursor mCursor; private boolean mReleased; private boolean mUpdated; } private final ArrayList<ManagedCursor> mManagedCursors = new ArrayList<ManagedCursor>(); // protected by synchronized (this) int mResultCode = RESULT_CANCELED; Intent mResultData = null; private boolean mTitleReady = false; private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE; private SpannableStringBuilder mDefaultKeySsb = null; protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused}; private final Object mInstanceTracker = StrictMode.trackActivity(this); private Thread mUiThread; final Handler mHandler = new Handler(); /** Return the intent that started this activity. */ public Intent getIntent() { return mIntent; } /** * Change the intent returned by {@link #getIntent}. This holds a * reference to the given intent; it does not copy it. Often used in * conjunction with {@link #onNewIntent}. * * @param newIntent The new Intent object to return from getIntent * * @see #getIntent * @see #onNewIntent */ public void setIntent(Intent newIntent) { mIntent = newIntent; } /** Return the application that owns this activity. */ public final Application getApplication() { return mApplication; } /** Is this activity embedded inside of another activity? */ public final boolean isChild() { return mParent != null; } /** Return the parent activity if this view is an embedded child. */ public final Activity getParent() { return mParent; } /** Retrieve the window manager for showing custom windows. */ public WindowManager getWindowManager() { return mWindowManager; } /** * Retrieve the current {@link android.view.Window} for the activity. * This can be used to directly access parts of the Window API that * are not available through Activity/Screen. * * @return Window The current window, or null if the activity is not * visual. */
不断扩展自己的属性和方法;
同样的Service类也是:
public abstract class Service extends ContextWrapper implements ComponentCallbacks2 { private static final String TAG = "Service"; public Service() { super(null); } /** Return the application that owns this service. */ public final Application getApplication() { return mApplication; } /** * Called by the system when the service is first created. Do not call this method directly. */ public void onCreate() { } /** * @deprecated Implement {@link #onStartCommand(Intent, int, int)} instead. */ @Deprecated public void onStart(Intent intent, int startId) { } /** * Bits returned by {@link #onStartCommand} describing how to continue * the service if it is killed. May be {@link #START_STICKY}, * {@link #START_NOT_STICKY}, {@link #START_REDELIVER_INTENT}, * or {@link #START_STICKY_COMPATIBILITY}. */ public static final int START_CONTINUATION_MASK = 0xf; /** * Constant to return from {@link #onStartCommand}: compatibility * version of {@link #START_STICKY} that does not guarantee that * {@link #onStartCommand} will be called again after being killed. */ public static final int START_STICKY_COMPATIBILITY = 0; /** * Constant to return from {@link #onStartCommand}: if this service's * process is killed while it is started (after returning from * {@link #onStartCommand}), then leave it in the started state but * don't retain this delivered intent. Later the system will try to * re-create the service. Because it is in the started state, it will * guarantee to call {@link #onStartCommand} after creating the new * service instance; if there are not any pending start commands to be * delivered to the service, it will be called with a null intent * object, so you must take care to check for this. * * <p>This mode makes sense for things that will be explicitly started * and stopped to run for arbitrary periods of time, such as a service * performing background music playback. */ public static final int START_STICKY = 1; /** * Constant to return from {@link #onStartCommand}: if this service's * process is killed while it is started (after returning from * {@link #onStartCommand}), and there are no new start intents to * deliver to it, then take the service out of the started state and * don't recreate until a future explicit call to * {@link Context#startService Context.startService(Intent)}. The * service will not receive a {@link #onStartCommand(Intent, int, int)} * call with a null Intent because it will not be re-started if there * are no pending Intents to deliver. * 。。。。。。
Application类同样:
public class Application extends ContextWrapper implements ComponentCallbacks2 { private ArrayList<ComponentCallbacks> mComponentCallbacks = new ArrayList<ComponentCallbacks>(); private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks = new ArrayList<ActivityLifecycleCallbacks>(); /** @hide */ public LoadedApk mLoadedApk; public interface ActivityLifecycleCallbacks { void onActivityCreated(Activity activity, Bundle savedInstanceState); void onActivityStarted(Activity activity); void onActivityResumed(Activity activity); void onActivityPaused(Activity activity); void onActivityStopped(Activity activity); void onActivitySaveInstanceState(Activity activity, Bundle outState); void onActivityDestroyed(Activity activity); } public Application() { super(null); }
最后让我们记住支撑“装饰模式”的设计原则:
Classes should be open for extension, but closed for modification.
自己可以看操作文件的Java源代码,设计思想也是应用到“装饰模式”,祝大家元旦快乐~~~
相关文章推荐
- Android源码学习之装饰模式应用
- Android源码学习之装饰模式应用
- Android源码学习之装饰模式应用
- Android源码学习之装饰模式应用
- Android源码学习之装饰模式应用
- Android源码学习之组合模式应用
- Android源码学习之观察者模式应用
- Android源码学习之单例模式应用及优点介绍
- Android源码学习之组合模式定义及应用
- Android源码学习之设计模式应用小整理
- Android源码学习之单例模式应用
- Android源码学习之观察者模式应用
- Android源码学习之模板方法模式应用
- Android源码学习之观察者模式应用及优点介绍
- Android源码学习之工厂方法模式应用
- Android源码学习之模板方法模式应用
- Android 4.0.3 联系人(通讯录)应用源码学习
- 【Android源码学习】应用启动过程
- Android学习博客列表(针对全面源码,framwork,应用)(1)
- Android应用开发商业模式学习