Application Design pattern in Java -- Singleton

Source: Internet
Author: User
This article introduces the basic concepts of Singleton in the design mode, briefly analyzes its functions and uses, lists several methods for implementing Singleton, and provides detailed Java code.
Basic concepts
Singleton is a creation model that ensures that only one instance is generated and provides a global access point to access it. for some classes, it is very important to ensure only one instance. For example, sometimes database connections or Socket connections are restricted, and only one connection can exist at a time. for another example, the set in the set cannot contain duplicate elements. The objects added to the set must be unique. If the repeated values are added to the set, only one instance is accepted. JDK uses the Singleton mode to implement the set feature. You can refer to java. util. the original code of the internal static class SingletonSet in Collections. in fact, Singleton is the simplest but also one of the most widely used models, which can be seen everywhere in JDK.
Simple analysis
To implement the Singleton mode, we need a static variable to remember whether an instance has been generated without creating an object. static variables or static methods can be called without generating a specific instance. Such variables or methods will not change because of class instantiation. in the structure shown in Figure 1, we can see that uniqueInstance is an independent static variable. It can remember whether the object has been instantiated and judge this variable in the static method Instance, A new object is generated if it has not been instantiated. If it has already been instantiated, no new object is generated and the previously generated instance is still returned.
Figure 1: Singleton mode structure
Implementation
There are usually three ways to implement the Singleton mode.
1. Use static methods to implement Singleton
This method monitors instance creation using static methods. To avoid creating more than one instance, we 'd better declare the constructor as private.
This prevents client programmers from creating an instance in any way except the methods provided by us. If The constructor is not declared as private, the compiler automatically synchronizes a default friendly constructor. this implementation method is the most common, that is, the standard implementation of the structure in figure 1.
Public class Singleton {
Private static Singleton s;
Private Singleton (){};

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.