Exploring the singleton mode of Android Development (source code sharing)

Source: Internet
Author: User

Exploring the singleton mode of Android Development (source code sharing)

What is the singleton mode? It is one of the models in Java 23 and is also widely used in Android development. For example, the Calander class does not use new Calander but Calander when creating objects. getInstnce () is an application of the singleton mode. What are the application scenarios? In many cases, we only need one object, but we do not need multiple objects. For example, the ancient emperor, a mountain cannot be a tiger. If there is more tigers, the only harm is not good. It is used to ensure that there is only one and only one instance in the entire application. one is all!

The Singleton mode is divided into two types: the hungry and the lazy. Let's take a look at what the hunger and lazy modes are!

Let's take a look at the true sense of the hunger model!

Public class Singleton {// 1. privatize the constructor and do not allow external users to directly create the object private Singleton () {}// 2. create a unique instance of the class and use private static to modify private static Singleton instance = new Singleton (); // 3. provides a method for getting an instance, using public static to modify public static Singleton getInstance () {return instance ;}}
In order not to allow this class to construct multiple objects, the first step of the hungry Chinese mode is to privatize the constructor, and external objects cannot be directly created. Step 2: create a unique instance of the class and use the private static modification to return the instance through getInstance (). This ensures that the class cannot be modified at will. The biggest feature of the hunger mode is that objects are created as long as the class starts to load.

Let's take a look at the lazy model.

Public class Singleton2 {// 1. privatize the constructor and do not allow external users to directly create the private Singleton2 () {}// 2. the unique instance of the Declaration class. Use private static to modify private static Singleton2 instance; // 3. provides a method to obtain an instance. Use public static to modify public static Singleton2 getInstance () {if (instance = null) {instance = new Singleton2 ();} return instance ;}}

We can see that the lazy and ELE. Me modes are roughly the same. The only difference is that only declaring classes in step 2 does not create objects.

Finally, we will summarize the differences between the two:

The hungry Chinese mode is characterized by slow loading of classes, but fast object retrieval speed and thread safety during running.
The lazy mode is characterized by fast loading of classes, but slow speed of getting objects during running, and unsafe threads. Pay attention to multithreading!


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.