Application of design Patterns in Java Singleton

Source: Internet
Author: User
Tags constructor static class

Basic concepts

Ingleton 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 important to ensure that only one instance is available, such as when a database connection or Socket connection is limited, and only one connection must be maintained at the same time.

For example, a set in a set cannot contain duplicate elements, and an object added to a set must be unique, and if a duplicate value is added to set, it accepts only one instance. The JDK formally uses the singleton mode to implement this feature of set, and you can view the original code of the internal static class Singletonset in Java.util.Collections. In fact, Singleton is one of the simplest but most widely used models, and is ubiquitous in the JDK.

Simple analysis

In order to implement the Singleton pattern, we need a static variable that can remember if an instance has been created without creating an object. Either a static variable or a static method can be called directly without producing a specific instance, and such a variable or method will not change because of the instantiation of the class.

As you can see in the structure of Figure 1, Uniqueinstance is the independent static variable that can remember whether an object has been instantiated, judge the variable in a static method Instance, and produce a new object without instantiating it. If it is already instantiated, the new object is no longer generated, and the previously generated instance is still returned.

Figure 1:singleton Pattern Structure

Specific implementation

There are usually three ways to implement the Singleton model.

One, the static method realizes Singleton

This method uses a static method to monitor the creation of instances. To prevent more than one instance from being created, we'd better declare the constructor private.

This prevents the client programmer from creating an instance in any way other than the one provided by us, and if the constructor is not declared private, the compiler will be smart to automatically synchronize a default friendly constructor. This implementation is the most common, the standard implementation of the structure in Figure 1.

public class Singleton
{
private static Singleton s;
private Singleton()
{
};
/**
* Class method to access
the singleton instance of the class。
*/
public static Singleton getInstance()
{
if (s == null)
s = new Singleton();
return s;
}
}
// 测试类
class singletonTest
{
public static void main(String[] args)
{
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if (s1==s2)
System.out.println
("s1 is the same instance with s2");
else
System.out.println
("s1 is not the same instance with s2");
}
}

Singletontest Run results are:

S1 is the same instance with S2

This proves that we have only created one instance.

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.