Singleton mode:
The singleton pattern, as its name implies, has only one instance and is responsible for creating its own objects. This class provides a way to access its unique objects directly, without instantiating the objects of that class
The singleton mode belongs to the create type
The actual code I put on Github: github.com/liangtengyu…
Application Scenarios:
Learning about a design pattern can help us understand it faster by understanding its usage scenarios,
The singleton pattern allows only one object to be created, thus saving memory and speeding up object access. Therefore, objects need to be suitable for use in common situations, such as multiple modules using the same data source to connect objects, and so on. Such as:
- Objects that need to be frequently instantiated and then destroyed.
- An object that takes too much time or resources to create but is often used.
- Stateful tool class objects.
- Objects that frequently access databases or files.
Implementation method:
The hungry type
public class Singleton_3 {
// Use hanky-panky thread safety
private static Singleton_3 instance = new Singleton_3() ;
private Singleton_3(a) {}public static Singleton_3 getInstance(a) {
returninstance; }}Copy the code
Slacker – Thread-safe
public class Singleton_2 {
// Using lazy thread safety is not recommended
private static Singleton_2 instance =null ;
private Singleton_2(a) {}public synchronized static Singleton_2 getInstance(a) {
if(instance ! =null) return instance;
return newSingleton_2(); }}Copy the code
Slacker – Not thread-safe
public class Singleton_1 {
// Using lazy is not thread-safe
private static Singleton_1 instance =null ;
private Singleton_1(a) {}public static Singleton_1 getInstance(a) {
if(instance ! =null) return instance;
return newSingleton_1(); }}Copy the code
Static class mode
public class sigleton0 { // Use static classes to implement singletons
private static ConcurrentHashMap cache = new ConcurrentHashMap();
}
Copy the code
Inner class mode
public class Singleton_4 {
// Use inner class to construct singletons, thread-safe and lazy to load
private AtomicInteger id = new AtomicInteger(0);
private Singleton_4(a) {}public static Singleton_4 getInstance(a){
return SingletonCreator.singleton_4;
}
private static class SingletonCreator{
static Singleton_4 singleton_4 = new Singleton_4();
}
public Integer getIncrementId(a){
return this.id.getAndIncrement();
}
Copy the code
Double check lock mode
public class Singleton_5 {
// Use a double lock to verify thread safety
private static Singleton_5 instance =null ;
// satisfy lazy loading
private Singleton_5(a) {}public static Singleton_5 getInstance(a) {
if(instance ! =null) return instance;
synchronized (Singleton_5.class) {
if (instance == null) {
return newSingleton_5(); }}return newSingleton_5(); }}Copy the code
Atomic class scheme
public class Singleton_6 {
// Use atomicRefence to support lazy loading using CAS mode
private static AtomicReference<Singleton_6> INSTANCE = new AtomicReference<Singleton_6>();
private Singleton_6(a) {}public static Singleton_6 getInstance(a){
for(; ;) { Singleton_6 singleton_6 = INSTANCE.get();if (null! = singleton_6)return singleton_6;
INSTANCE.compareAndSet(null.new Singleton_6());
returnINSTANCE.get(); }}Copy the code
The experiment
The result of getting a singleton is actually the same object every time
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
// Get the instance
Singleton_4 instance = Singleton_4.getInstance();
// Output address
System.out.println("Instance address :" + instance);
/ / to get id
System.out.println(instance.getIncrementId());
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); }}Copy the code
Results:
Instance address :Singleton_4@63947c6b0-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- instance address: 63947 c6b Singleton_4 @1-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- instance address: 63947 c6b Singleton_4 @2-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- instance address: 63947 c6b Singleton_4 @3-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -...Copy the code
Concern public number: Java baodian