Explain Java singleton mode with pictures and texts
PS: first of all, we need to know what a singleton is, why it is used, and what are the benefits of using it.
1: Singleton pattern is a common design pattern in Java. There are several ways to write singleton patterns. Here we mainly introduce two kinds: lazy singleton and hungry singleton
The singleton mode has the following characteristics:
1. A singleton class can only have one instance. 2. A singleton class must create its own unique instance. 3. The singleton class must provide this instance to all other objects.
The singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the whole system. In the computer system, the driver objects of thread pool, cache, log object, dialog box, printer and graphics card are often designed as singletons. These applications have more or less the function of resource manager. Each computer can have several printers, but only one printer spooler to avoid two print jobs being output to the printer at the same time. Each computer can have several communication ports. The system should centrally manage these communication ports to avoid one communication port being called by two requests at the same time. In short, the singleton mode is selected to avoid inconsistency and political bullshit.
2: Lazy style
Write out the singleton class first
Here directly on the code, the code has a detailed explanation
As can be seen from the above figure, even if more objects are created, there is only one singleton object instance at the bottom, and the string representation of the created objects is the same. Some people must have questions. What are the usual two objects like? I'll explain it to you below. Before that, I'll write about what to pay attention to in this lazy style, It is thread unsafe. Multiple singleton instances are likely to occur in a concurrent environment. There are many methods to solve them, such as synchronization locks, static internal classes, etc. Here we mainly talk about static internal classes. This is better,
Call:
Result diagram:
Here, I also created two objects to illustrate that the magic thing is that I printed the singleton creation twice. Is this another successfully created object? The answer is: although it has been printed twice and there are two object names, the string representation of the object is still the same, and everyone knows the usage of static, that is, the singleton object has been created while the class is loaded, and will not be created later, Even if you call getInstance () later, the underlying singleton object is still public
Similarly, I wrote a common class to create two objects at the same time and print their toString () methods, as follows:
It can be seen that the string representation of the object is different
3: Hungry Han style single case
Hungry Han style single instance class When the class is initialized, it has been instantiated by itself
Because this is a static modified method, it is created when the class is loaded and will not change later, so the thread is safe.
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.