Android design mode series (3)--SDK source code Single example mode

Source: Internet
Author: User

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
  1. Public Final class Inputmethodmanager {
  2. static Final Object Minstancesync = new Object (); Sync
  3. //Internal globally unique instance
  4. static Inputmethodmanager minstance;
  5. //External API
  6. static public Inputmethodmanager getinstance (context context) {
  7. return getinstance (Context.getmainlooper ());
  8. }
  9. /** 
  10. * Internal API for the above external API calls
  11. * @hide System-Hidden API
  12. */
  13. static public Inputmethodmanager getinstance (Looper mainlooper) {
  14. synchronized (minstancesync) {
  15. if (minstance! = null) {
  16. return minstance;
  17. }
  18. IBinder B = Servicemanager.getservice (Context.input_method_service);
  19. Iinputmethodmanager service = IInputMethodManager.Stub.asInterface (b);
  20. Minstance = New Inputmethodmanager (service, mainlooper);
  21. }
  22. return minstance;
  23. }
  24. }

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

Java code
  1. Class Contextimpl extends context{
  2. @Override
  3. Public Object Getsystemservice (String name) {
  4. if (window_service.equals (name)) {
  5. //... ... Omit the following n if,else if
  6. } Else if (input_method_service.equals (name)) {
  7. //Get unique instance of Input method manager
  8. return inputmethodmanager.getinstance (this);
  9. } Else if (keyguard_service.equals (name)) {
  10. //... ... Omit the following n if,else if
  11. } Else if (accessibility_service.equals (name)) {
  12. //See also single case, everywhere
  13. return accessibilitymanager.getinstance (this);
  14. } Else if (location_service.equals (name)) {
  15. //... ... Omit the following n if,else if
  16. } Else if (nfc_service.equals (name)) {
  17. return Getnfcmanager ();
  18. }
  19. return null;
  20. }
  21. }

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

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.