Single case model of Android design pattern

Source: Internet
Author: User

In design mode, the simplest is the single case mode. Let's take a look at the single case mode

Original: http://www.iteye.com/topic/575052

The singleton pattern can be simple, and all it takes is one class to complete (see this chapter's poor UML diagram). However, if the "number of objects created and when they are created" is to be taken seriously, the singleton pattern can be quite complex, complicated by the addition of the first five modes, such as the discussion of DCL double lock detection (double checked locking), Involves multiple ClassLoader (ClassLoader) at the same time, involving across the JVM (cluster, remote EJB, etc.), involving a single instance of the object was destroyed after the reconstruction.

Objective:

You want the object to create only one instance and provide a global access point.

Figure 6.1 UML diagram for a single example pattern

Structure is simple, but there is a situation;

1. Each time from getinstance () can return one and only one object.

2. In the case of resource sharing, getinstance () must accommodate multithreading concurrent access.

3. Improve access performance.

4. Lazy load (Lazy load) is constructed when needed.
first, implement the single example mode A in 1: [Java]  View plain copy 4.public class singletona {     5.       6.    /**   7.     *  Single Example object instance    8.     */     9.    private static  singletona instance = null;     10.      11.     public static singletona getinstance ()  {     12.         if  (instance == null)  {                                //line 12     13.             instance = new singletona ();           //line 13     14.         }     15.        return  instance;     16.    }     17.}     
This writing we have four points from the above detection, found that the 2nd time there is a problem, assuming such a scenario: two threads concurrent call Singleton.getinstance (), assuming that the line Cheng first judge whether the instance is null, both lines in the code 12 Enter the position of line 13. Just after the decision was made, the JVM switched CPU resources to thread two, and since line Cheng has not yet been executed by lines 13, instance is still empty, so thread two performs the new Signleton () operation. After a moment, the line Cheng is awakened, and it is still performing the new Signleton () operation. So the single case pattern of this design does not meet the 2nd requirement.

Here we go.
implementation of single case mode in 2 B: [Java]  View plain copy 4.public class singletonb {     5.       6.    /**   7.     *  Single Example object instance    8.     */     9.    private static  singletonb instance = null;     10.      11.     public synchronized static singletonb getinstance ()  {      12.        if  (instance == null)  {      13.            instance  = NEW SINGLETONB ();     14.         }     15.        return instance;     16.    }     17.}     
Instead of a single synchronized modifier in a method, it is now guaranteed that no thread problems will be made. But there is a very large (at least time-consuming) performance problem here. In addition to executing the SINGLETONKERRIGANB constructor on the first call, each subsequent call returns the instance object directly. Returning an object is a very small operation, and most of the time it takes to prepare the synchronized modifier for synchronization, so it is not cost-effective to perform.

Implement 3 single case pattern C: [Java]View Plain Copy 4.public

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.