Combination mode
Android in the combination of the application of the model is flooded into porridge, everywhere, that is the use of the view and ViewGroup class. In the Android UI design, almost all widgets and layout classes rely on these two classes.
The combination mode, Composite pattern, is a very ingenious pattern. Almost all object-oriented systems are applied to the combined mode.
Combines object view and viewgroup into a tree structure to represent a "partial-whole" hierarchy (view can be a part of ViewGroup).
The combined mode makes the user consistent with the use of single object view and composite object ViewGroup.
Builder mode
In Android, you often use Alertdialog to display some simple windows, usually in the following code:
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("提示") .setMessage("确定退出?").create(); dialog.show();
You can see that dialog was created through builder, so how does builder work with Alertdialog? In fact, the creation of Alertdialog is the use of builder mode, not much nonsense to say, directly on the code bar.
Memo Mode
The onsaveinstancestate and onrestoreinstancestate of activity is to store the state of activity through the serialized data structure of the bundle. So that you can restore the information that the activity saved before ondestory.
State mode
The View.onvisibilitychanged method is to provide a state pattern implementation that allows actions in the Onvisibilitychanged method to be raised when the view's visibility changes. For example, when the gone,visible state of a view changes, the behavior changes.
Single-Case mode
Android has a lot of system-level global variables, such as time, input method, account, status bar and so on, which are directly or indirectly used by Android in the single-case mode.
Observer pattern
In Android, the most typical observer pattern is "broadcast."
(1) Timelinereceiver is the observer;
(2) Rewrite the OnReceive method to define the subscriber's behavior when the state change is received;
class TimelineReceiver extends BroadcastReceiver {// 1 @Override publicvoid// 2 // 3 // 4 Log.d("TimelineReceiver""onReceived"); } }
Command mode
Many multithreaded multi-processes are used in Android, so it is necessary to use a lot of runbable,thread. In fact, the simplest mode of command is used.
publicinterface Runnable { publicabstractvoidrun();}
Caller Thread Lite Code:
//Command mode here does not need to inherit the Runnable interface, but here to consider the actual situation, such as convenience, inherited the runnable interface, the implementation of the Run method, this is the thread itself running Run method class Thread implements Runnable { PrivateRunnable Target; PublicThread (Runnable target) { This. target = target; } PublicSynchronizedvoidStart () {if(Threadstatus! =0|| This! = Me)Throw NewIllegalthreadstateexception ();Group. Add ( This); Start0 ();//This is a local method, call the Run method if(Stopbeforestart) {stop0 (throwablefromstop); } }//Optional Public voidRun () {if(Target! =NULL) {Target.run (); } }}
The client only needs new Thread (new Runnable () {}). Start () starts executing a series of related requests, most of which are anonymous classes that implement the Runnable interface.
Broker mode
The implementation logic of the Resource.getdrawable method is this: Create a cache to hold all of the loaded, if the getdrawable passed in the ID corresponding to the drawable has not been loaded before, then it will be based on the ID of the resource type, respectively, call the XML parser generated , or create a drawable by reading the picture resource file in the package.
and resource.getdrawable the role of a mediator by encapsulating operations involving multiple objects and multiple logic into a single method.
Proxy mode
Assuming that an activity a goes to bind a service s, each method in a call to S is actually brokered by the system's binder mechanism, and then calls the corresponding method in S. The binder mechanism acts as an agent.
Adapter mode
For Android developers, the adapter mode is just too familiar, however, there are many applications that can be said to use the adapter mode, such as a ListView, directly or indirectly, every day.
The ListView is used to display list data, but as a list data collection there are many forms, there are arrays, there are cursor, we need the corresponding adapter as a bridge, processing the corresponding data (and can form the view required by the ListView).
It is precisely because these adapter interfaces and adapter classes are defined that our data is easily and flexibly displayed on the Adapterview implementation class.
Design patterns involved in android:android