A probe into the single-instance mode of Android development (source code sharing)

Source: Internet
Author: User

What is a singleton mode? is one of the Java23 mode, in the development of Android also has a wide range of applications, such as Calander class, when creating objects is not through the new calander but Calander.getinstnce (), this is a single-instance mode of application, What is the application of the occasion? We have a lot of time some objects only need one is enough, do not need more than a few, such as the ancient Emperor, a mountain not two tigers, Tigers more than only the disadvantage of no good. Its role is to ensure that an instance of the entire application has only one, and that is all!

The singleton mode is divided into two kinds of one is the A hungry man mode one is the lazy mode, let us first look at the A Hungry man mode and the lazy mode is what to put!

Let's take a look at the A hungry man pattern!

public class Singleton {//1. Privatize the construction method and do not allow external direct creation of the object private Singleton () {}//2. Create a unique instance of a class, using private static to decorate private static Singleton instance=new Singleton ()//3. Provides a method for obtaining an instance, using public static to decorate public static Singleton getinstance () {return instance;}}
In order not to allow the class to construct multiple objects, the first step in a hungry man mode is to privatize the construction method and not allow the external creation of objects directly. The second step is to create a unique instance of the class, and return it with the private static adornment, by getinstance (), to ensure that the calling class cannot modify the class arbitrarily. The biggest feature of the A Hungry man mode is that the object is created as soon as the class starts to load.

And look at the lazy pattern.

public class Singleton2 {//1. Privatization of construction, not allowing direct creation of objects outside private Singleton2 () {}//2. Declaring a unique instance of a class, using private static to decorate private static Singleton2 INSTANCE;//3. Provides a method for obtaining an instance, using public static to modify public static Singleton2 getinstance () {if (instance==null) { Instance=new Singleton2 ();} return instance;}}

We can see that the lazy pattern and the A hungry man pattern are roughly the same, except that in the second step, only the class is declared and the object is not created.

Finally, we summarize the difference between the two:

A hungry man mode is characterized by the slow loading of classes, but the faster the runtime gets objects and thread safety.
Lazy mode is characterized by faster loading of classes, but slower time to get objects at runtime and unsafe threads. Multi-threaded operation should pay attention to!


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.