Brief introduction to Java singleton mode

1、 Concept

Singleton pattern is a common software design pattern. Its core structure contains only a special class called singleton class. The single instance mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, so as to facilitate the control of the number of instances and save system resources. If you want only one object of a class to exist in the system, singleton mode is the best solution. In my opinion, a single example is not to let the outside world create objects.

1.1 concept analysis

For a single example, from the above concept analysis, the following conditions should be met:

First: there can only be one singleton object in a singleton class;

Second: the singleton class must create its own unique instance object;

Third: the instance object can be accessed by the outside world, and the outside world cannot create the object itself.

2、 Several common singleton modes

In Java, the singleton mode is generally divided into lazy type, hungry type and registration type, but the registration type is generally rarely seen, so it is easy to ignore. If I hadn't suddenly wanted to sum up today and looked for information on the Internet, I wouldn't have noticed this. The code is posted and explained in this way.

2.1 hungry Chinese single instance

Write a test class to test whether the singleton is implemented:

As can be seen from the above, the two references of this test class are equal, that is, the two references point to the same object, which also conforms to the singleton pattern standard. Here, the hungry man introduction ends.

2.2 hungry Chinese single instance

Test class:

As can be seen from the above, the two references of this test class are equal, that is, the two references point to the same object, which also conforms to the singleton pattern standard. Here, the lazy introduction ends.

2.3 difference between lazy and hungry

Lazy means that when there is no object, a singleton object will be created. When there is an object, no object will be created. This may not be so easy to understand, but if readers are interested in learning more, they can use breakpoints in eclipse to test, add breakpoints to the contents in the if curly braces of lazysingleton class, and then in the test class, Run with debug, so that the child can be easily reflected. An object is created for the first time, but no object is created for the second time.

The hungry Chinese method is to use the keyword final to create the object. When the caller needs an instance object, he can obtain the created instance through the getInstance method.

2.4 registered single instance

For the registered single example class, the author is not very familiar with it. He pasted a section of code on the network for his own learning and reference. Please learn by yourself.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>