Android Development Singleton mode

Source: Internet
Author: User

What is this singleton mode? It is a pattern between Java23 and is also widely used in Android development, example Calander category, when the object is created by not creating new calander but rather calander.getinstnce (), which is an application of the singleton pattern, What is the application of the occasion? We in very many times some objects just need one is enough, do not need more than, for example, the ancient emperor, a mountain not two tigers, Tigers more than just have the disadvantage of no merit.

Its role is to ensure that an instance of the entire application has and only one. One 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 of what each!

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

public class Singleton {//1. Privatize the construction method, and does not agree to externally create the object directly 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. Use public static to decorate public static Singleton getinstance () {return instance;}}
In order to not allow the class to construct multiple objects. The first step in a hungry man mode is to privatize the construction method. Do not agree to create objects directly externally.

The second step then creates a unique instance of the class and modifies it with private static. Returned by getinstance () ensures that the calling class cannot make any changes to the class.

The biggest feature of the A Hungry man mode is that the object is created only when the class starts loading.

And look at the lazy pattern.

public class Singleton2 {//1. Privatization of construction, not agreeing to create objects directly outside the 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. Use public static to decorate 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 simply declaring the class does not create the object.

Finally, we summarize the difference between the two:

A hungry man mode is characterized by slower loading of classes. However, it is faster and thread-safe to get objects at execution time.


The lazy pattern is characterized by the faster loading of classes, but the slower the object gets when executed. Thread safety. Be careful when you are working with multithreading.


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android Development Singleton mode

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.