Singleton and androidsingleton in Android Design Mode

Source: Internet
Author: User

Singleton and androidsingleton in Android Design Mode
I. Overview

The Singleton mode is the simplest one in the design mode, but it does not have the abstract relationship between various objects in the design mode. Therefore, some people do not think it is a mode, it is an implementation technique. the Singleton mode provides an instance that can only be instantiated by itself, and provides a global access point. three requirements must be met: private constructor (to prevent being instantiated by others), static private object (to provide instances ), static public getInstance method (used to create and obtain instance objects ).
Advantages and disadvantages: a single instance only allows you to create one instance by yourself. You do not need to create or destroy instances frequently, which can save memory and speed up object access. however, the Singleton does not have an abstraction layer or excuse, which is inconvenient to expand. the Singleton provides both the factory method and the business method, which violates the single responsibility principle to a certain extent.
Ii. Singleton implementation

The implementation of Singleton has two mainstream Methods: lazy mode and ELE. Me mode. They differ in the timing and efficiency of Instantiation.


1. Lazy Mode

/*** Created by jesse on 15-6-28. */public class Singleton {private static Singleton instance; private Singleton () {}// you must have a private structure. Do you want to discuss the case of public static Singleton getInstance () {if (null = instance) {instance = new Singleton () ;}return instance ;}}


In lazy mode, when obtaining an instance from an external object, you must first determine whether the instance is initialized. In this mode, the system will lose some efficiency but save some space, the reason is when the instance will be used for initialization. If it cannot be used all the time, the object will not be constructed in lazy mode. it is equivalent to changing the time using space. that is, the delay loading technology. another thing to note is that there must be a private structure. Otherwise, the external object can still be instantiated.


At this time, many of our friends have said that you have a problem with this lazy ticket and thread security issues. of course, this method is thread unsafe. When two threads A and B concurrently need to obtain the instance, the instance has not been initialized. Suppose A obtains the time slice to initialize the instance first, when the instance has not been initialized, the time slice is given to B. At this time, because the instance has not been initialized, the instance is still blank, so B will instantiate the instance again, the final result is that the instance is instantiated twice. this may cause serious consequences. The solution is to lock (sacrifice some performance) and use the hunger mode.


Lock the getInstance method. This locking method can solve the thread security problem. However, every time an external object wants to obtain an instance, it needs to lock the thread and then determine whether the instance has been instantiated, in this way, the efficiency loss is considerable in the case of multi-thread and high concurrency.

Public class Singleton {private static Singleton instance; private Singleton () {} public static synchronized Singleton getInstance () {// lock the getInstance method if (null = instance) {instance = new Singleton ();} return instance ;}}

Later, a double-check lock instance method was extended. This method only locks the instance during the first instantiation and determines whether to instantiate the instance before and after the lock. the performance is superior to the single check lock. however, this double check lock may also encounter a problem, that is, errors may occur on different platforms or compilers. this is mainly because some compilers have optimized the process of new Singleton (). Assuming that the thread AB obtains instances concurrently, Some compilers may encounter A thread instantiating the instance, when the system has allocated memory to the instance but has not initialized the member variable of the instance, the time slice is given to the B process. At this time, the instance is no longer null, B takes the instance directly to operate the member variable, but the member variable has not been initialized at this time, and the result may be crash. when such a compiler problem occurs, you need to add volatile to the instance variable to remove the interference caused by Compiler optimization.

Public class Singleton {private static Singleton instance; // private static volatile Singleton instance; use private Singleton () {} public static Singleton getInstance () in case of compiler Problems () {if (null = instance) {synchronized (Singleton. class) {if (null = instance) instance = new Singleton () ;}} return instance ;}}


2. Hunger Mode


In the hungry Chinese mode, the system runs and initializes the object when it is installed in the class. external objects can be directly used without any judgment, efficiency is superior to lazy mode. however, compared with the lazy mode of delayed loading technology, the memory will be created at the beginning no matter the system uses this instance. the opposite is true for lazy's time-to-space change.

Public class Singleton {private static final Singleton instance = new Singleton (); private Singleton () {}// you must have a private structure () {return instance ;}}

Iii. Summary
According to the above analysis, we can clearly understand that the hungry Chinese Mode does not need to face the thread concurrency problem. in addition, the call speed and response time are superior to those of the lazy mode. however, from the perspective of resource utilization, the lazy mode is better than the hungry mode.
Reprinted please indicate the source: http://blog.csdn.net/l2show/article/details/46672061

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.