Definition: Singleton, single instance, that is, a class can only get one instance.
Categories: Hunger mode and Lazy mode (without thread safety considerations)
How to implement it: usually, when we get an instance of a class, we use new to get it once. How can we ensure that only one instance can be obtained?
1. First of all, we should ensure that we can not use new xxx() to obtain instances from outside, and privatize the construction method of xxx class.
2. Once the constructor is privatized, it is inaccessible to the outside world. It needs to provide a common method to obtain an instance of the class. Moreover, this method can not be called by the instance (it can not be called by creating an instance of the class), it should be a class method.
3. To ensure that the obtained instances are unique, you need to create instances as soon as the class is loaded. The class will only load once, and the instance will only be created once.
The way to realize the hungry man model is as follows:
/** * @ClassName: SingletonHunger * @Description: (Hungry Man Model) * @author: Ah Q * @date 2019 September 5th 2013 * */ public class SingletonHunger { //3. How to ensure the uniqueness of instances? When a class is loaded, it creates objects only once, which ensures that the instance is unique. private static SingletonHunger instance=new SingletonHunger(); //1. First, make sure that you can't use new XXX() to create instances, and set the constructor to be externally inaccessible private SingletonHunger(){ } //2. Externally inaccessible. How do you create instances? We need to provide an external method to get an instance through this method; moreover, this method can not be an instance method (can not create an instance to call a method), but can only be set as a class method. public static SingletonHunger getInstance(){ return instance; } }
Slacker mode implementation (without thread security considerations):
/** * @ClassName: SingletonLazy * @Description: (Lazy Man Model * @author: Ah Q * @date 2019 September 5th 2013 * */ public class SingletonLazy { private static SingletonLazy instance = null; private SingletonLazy(){} public static SingletonLazy getInstance(){ if(instance==null){ instance=new SingletonLazy(); } return instance; } }
Test whether the acquired instance is unique:
public class Test { public static void main(String[] args) { SingletonHunger singleton1 = SingletonHunger.getInstance(); SingletonHunger singleton2 = SingletonHunger.getInstance(); if(singleton1==singleton2){ System.out.println("true"); }else{ System.out.println("false"); } SingletonLazy singleton3 = SingletonLazy.getInstance(); SingletonLazy singleton4 = SingletonLazy.getInstance(); if(singleton3==singleton4){ System.out.println("true"); }else{ System.out.println("false"); } } }
Print results:
true true
The difference between the lazy model and the hungry model:
Hunger mode: Class once loaded will create instances, waste memory, thread safety.
Lazy mode: Instances are created only when they need to be used, but threads are not safe. The implementation of thread security in lazy mode needs to be supplemented.
To be added...