android--Single-Case mode

Source: Internet
Author: User

Detailed various modes

Http://mobile.51cto.com/android-419145.htm

Http://wenku.baidu.com/link?url= F3yjq6yvslvhcwjlbyktbu3d2k-geswohw7hqnvv6rjieuhzz-gw5p8kfrw2wo3t1picdsukn2agriy99tucxijdjx8yp4lf6gf5w2shfeu

Http://baike.baidu.com/link?url=oAR1a4M5t3vjl2ktev8cuOAOKnZwRNczmRX6ot6e8iHTpqRrwAOcpSQmF5ILnAqJEMKrV_ojIt7je0jVMgXkc_

Single-Case mode:

Android design mode series-Singleton mode

The singleton mode, which can be said to be the simplest of the 23 design patterns of GOF.

This mode is relatively independent compared to several other modes, it is only responsible for the control of their own number of instantiations of a single (rather than consider the user to produce what kind of instance), very interesting, is a feeling very clean mode, I like this mode.
In many parts of Android, a singleton model is used, this article takes the Input method manager Inputmethodmanager As an example, unfolds the analysis.
Single-case pattern, Singleton pattern, can be used to replace the global variables in the system with its unique advantages.

1. Intentions
Ensure that a class has only one instance and provides a global access point to access it.
Hot words: Singleton unique private structure

2. Structure

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.
Take the input method as an example, change to the actual situation:


Very simple, but a little, from the above we also saw the Synchronized keyword, in the multi-threaded environment, the singleton mode in order to ensure that the number of their own instances of the only, will certainly do concurrency control.
Similar to this thread-safe single case, cross-process single-case, parameterized Singleton, and so on, indeed beyond the scope of this article, and all involve a lot of things, is a very big topic, not to unfold.

3. Code

 Public Final classInputmethodmanager {Static FinalObject Minstancesync =NewObject ();//Sync//internal globally unique instances    StaticInputmethodmanager minstance; //External API    Static  PublicInputmethodmanager getinstance (context context) {returngetinstance (Context.getmainlooper ()); }             /*** Internal API for the above external API call * @hide System-Hidden API*/     Static  PublicInputmethodmanager getinstance (Looper mainlooper) {synchronized(minstancesync) {if(Minstance! =NULL) {                  returnminstance; } IBinder b=Servicemanager.getservice (Context.input_method_service); Iinputmethodmanager Service=IInputMethodManager.Stub.asInterface (b); Minstance=NewInputmethodmanager (service, mainlooper); }          returnminstance; }  } 

The client calls, such as the Getsystemservice () method in Contextimpl, are called as follows:

classContextimplextendscontext{@Override PublicObject Getsystemservice (String name) {if(window_service.equals (name)) {//... ... Omit the following n if,else if}Else if(input_method_service.equals (name)) {//get unique instance of Input method manager            returnInputmethodmanager.getinstance ( This); }  Else if(keyguard_service.equals (name)) {//... ... Omit the following n if,else if}Else if(accessibility_service.equals (name)) {//See also single case, everywhere            returnAccessibilitymanager.getinstance ( This); } Else if(location_service.equals (name)) {//... ... Omit the following n if,else if}Else if(nfc_service.equals (name)) {returnGetnfcmanager (); }          return NULL; }  }  

Very simple, clean one mode.

4. Effects
(1). Create model.
(2). Controlled access to the unique instance.
(3). Avoid polluting namespaces with global variables.
(4). Allows refinement of operations and representations.
(5). More flexible than class operations.

android--Single-Case mode

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.