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:
Java 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 calls
- * @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;
- }
- }
The client calls, such as the Getsystemservice () method in Contextimpl, are called as follows:
Java code
- 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 unique instance of Input method manager
- 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 case, 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. 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 design mode series (3)--SDK source code Single example mode