Android Design Pattern series (3)-SDK source code Singleton pattern

Source: Internet
Author: User

Singleton mode is the simplest of the 23 gof design modes.
This mode is relatively independent from other modes. It is very interesting to control the number of Instantiation instances (instead of considering the instances generated for users, it is a clean mode that I like very much.
In Android, the singleton mode is used in many places. This article uses inputmethodmanager, the Input Method Manager, as an example to analyze.
Singleton pattern can replace global variables in the system with its unique advantages. It is widely used.

1. Intention
Ensure that a class has only one instance and provides a global access point to it.
Popular Words:Unique private structure of a single instance

2. Structure

Android has many system-level global variables, such as time, input method, account, and status bar. Android directly or indirectly uses Singleton mode.
Take the input method as an example to change it to the actual situation:


It is very simple, but we also see the synchronized keyword from the above. In a multi-threaded environment, the singleton mode is bound to implement concurrency control to ensure the unique number of instances.
Such thread-safe Singleton, cross-process Singleton, parameterized Singleton, and so on are indeed beyond the scope of this article and involve many things, it is a big topic and it is difficult to expand.

3.Code:

Public final class inputmethodmanager {static final object minstancesync = new object (); // synchronization // The globally unique internal instance static inputmethodmanager minstance; // The external API static public inputmethodmanager getinstance (context) {return getinstance (context. getmainlooper ();}/*** internal API, which is used to call the preceding external API * @ hide system hidden API */static public inputmethodmanager getinstance (Looper mainloz) {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, mainlogoff);} return minstance ;}}

For example, the getsystemservice () method in contextimpl is called as follows:

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) {// obtain the unique instance return inputmethodmanager of the Input Method Manager. getinstance (this);} else if (keyguard_service.equals (name )){//...... omit the following n if, else if} else if (accessibility_service.equals (name) {// For details, see the singleton. Return accessibilitymanager is everywhere. 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 ;}}

A very simple and clean mode.

4. Results
(1). Create mode.
(2) controlled access to a unique instance.
(3) avoid global variables from polluting the namespace.
(4). Allows refinement of operations and representations.
(5). more flexible than class operations.

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.