Android design Pattern Series of single case mode _android

Source: Internet
Author: User

Single case mode, which can be said to be the simplest of the 23 design patterns of GOF.

This pattern is relatively independent of several other modes, it is only responsible for controlling its own instantiation quantity single (instead of considering what kind of instance produces for the user), it is very interesting, it is a very clean mode, I like this mode very much.
In many parts of Android, the single case mode is used, this article takes the Input method manager Inputmethodmanager As an example, launches the analysis.
Single case mode, Singleton pattern, can be used to replace the global variable in the system with its unique advantages.

1. Intent

Guarantees that a class has only one instance and provides a global access point to access it.
Hot words: Single example unique private constructs

2. Structure

Android has a number of system-level global variables, such as time, input method, account, status bar, etc., which are directly or indirectly used by Android in a single example.

Take the input method as an example, revise the above picture to the actual situation:

Very simple, but there is a point, from the above we also saw the Synchronized keyword, in a multi-threaded environment, the single example mode in order to ensure that the number of their own instances of the only, will do concurrency control.

A single example of this thread safety, a single case across the process, parameterized single cases, and so on, indeed beyond the scope of this article, but also involves a lot of things, is a very big topic, not open.

3. Code:

Public final class Inputmethodmanager { 
static final Object Minstancesync = new Object ()//sync 
/Internal globally unique instance 
Static Inputmethodmanager minstance; 

External API 
static public Inputmethodmanager getinstance (context context) {return 
getinstance ( Context.getmainlooper ()); 
} 

/** 
* Internal API for the above external API call 
* @hide system hidden API 
/static public Inputmethodmanager getinstance (looper Mainlooper) { 
synchronized (minstancesync) { 
if (minstance!= null) {return 
minstance; 
} 
IBinder B = Servicemanager.getservice (context.input_method_service); 
Iinputmethodmanager service = IInputMethodManager.Stub.asInterface (b); 
Minstance = new Inputmethodmanager (service, mainlooper); 
} 
return minstance; 
} 

Client calls, such as those in the Getsystemservice () method in Contextimpl, are called:

Class Contextimpl extends context{ 
@Override public 
Object getsystemservice (String name) { 
if (window_ Service.equals (name)) { 
//... Omit the following n if,else if 
} else if (input_method_service.equals (name)) { 
//Get Input Method Manager unique instance return 
Inputmethodmanager.getinstance (this); 
} else if (keyguard_service.equals (name)) { 
//... Omit the following n if,else if 
} else if (accessibility_service.equals (name)) { 
//See also single example, everywhere return 
Accessibilitymanager.getinstance (this); 
} else if (location_service.equals (name)) { 
//... Omit the following n if,else if 
} else if (nfc_service.equals (name)) {return 
getnfcmanager (); 
} 
return null; 
} 

Very simple, clean one mode.

4. Effect

(1). Create type mode.

(2). Controlled access to a unique instance.

(3). Avoid global variable pollution namespaces.

(4). Allows refinement of operations and representations.

(5). More flexible than class operations.

The above is a small set to introduce the Android design Model series of single case mode, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.