What is a singleton mode?
Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system. The singleton schema definition in Java: "A class has and only one instance, and self-instantiation is provided to the system as a whole." ”
What is the meaning of the singleton pattern?
For some classes in the system, only one instance is important, for example, there can be multiple print tasks in a system, but only one task is working; a system can have only one window manager or file system; A system can have only one timing tool or ID (ordinal) generator. You can only open one task Manager in Windows. If you do not use the mechanism to unique window objects, will pop up more than one window, if these windows display exactly the same content, it is a duplicate object, wasting memory resources, if these windows display inconsistent content, it means that in a moment the system has multiple states, and the actual discrepancy, but also to the user misunderstanding, I don't know which one is the real state. Therefore, it is sometimes important to ensure that an object in the system is unique, that only one instance of a class is available.
Key points of the singleton model
The main points of the singleton pattern are three; one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.
From the specific implementation point of view, is the following three points: first, the Singleton mode class only provides a private constructor, the second is that the class definition contains a static private object of the class, and third, the class provides a static public function to create or get its own static private objects.
The form of a singleton pattern (two common types are described here)
1. The first form: lazy (thread insecure).
- Public class Singleton {
- private static Singleton instance;
- Private Singleton () {}
- public static Singleton getinstance () {
- if (instance = = null) {
- Instance = new Singleton ();
- }
- return instance;
- }
- }
Lazy (thread-safe). It is inefficient and is used in very few cases.
- Public class Singleton {
- private static Singleton instance;
- Private Singleton () {}
- public static synchronized Singleton getinstance () {
- if (instance = = null) {
- Instance = new Singleton ();
- }
- return instance;
- }
- }
2. Second form: A Hungry man type
- Public class Singleton {
- private static Singleton instance = new Singleton ();
- Private Singleton () {}
- public static Singleton getinstance () {
- return instance;
- }
- }
A hungry man and lazy-type difference
From the name of the A hungry man and the lazy,
A hungry man is the class once loaded, the singleton initialization is completed, to ensure that the getinstance, the singleton is already exist,
And lazy idle, only when the call getinstance, only to go back to initialize the singleton.
Java----Singleton mode