* What is the singleton pattern: * Single: only * example: instance * singleton design pattern, that is, a code pattern in which a class can only be obtained and used by one instance object in the entire system * * Design points: * 1. A class can only have one instance: * Constructor privatization * Second, it must create this instance by itself: * Contains a static variable of this class to save this unique instance * Third, it must provide this instance to the entire system by itself * The way to obtain the instance object externally: * 1 , direct exposure 2 , use static variable get to obtain * common two forms: hungry Chinese style, lazy style
1. Hungry mode
1. The first way
public class Singleton1 {
public static final Singleton1 INSTANCE = new Singleton1(); private Singleton1(){ } }
Second, the second way
Created by using enumeration, it is the simplest one in Hungry Mode
public enum Singleton2 {
INSTANCE }
Three, the third way
Use scenario: suitable for complex initial instantiation
public class Singleton3(){
private static final Singleton INSTANCE; static { INSTANCE= new Singleton3(); } private Singleton3(){ } }
Use: Load the initial value in a properties file under src
package com.example.springboot.test;
import java.io.IOException;
import java.util.Properties;
/**
* @Author chenduoduo * @create 2020/4/15 10:49 * 静态代码块的饿汉式 * 使用的场景:符合复杂实例化 */ public class Singleton3 { public static final Singleton3 INSTANCE; private String info; static { try { //加载properties文件 Properties pro =new Properties(); //使用类加载器加载,可以避免线程安全问题 pro.load(Singleton3.class.getClassLoader().getResourceAsStream("singleton.properties")); INSTANCE = new Singleton3(pro.getProperty("info")); } catch (IOException e) { throw new RuntimeException(e); } } private Singleton3(String info){ this.info = info; } @Override public String toString() { return "Singleton3{" + "info='" + info + '\'' + '}'; } }
Get value
public class SingletonTest {
public static void main(String[] args) { Singleton3 instance = Singleton3.INSTANCE; System.out.println(instance); } }
Two, lazy mode
1. The first way
* 1 , Privatization of the constructor * 2 , Use static variables to save the unique instance * 3 , Provide a static method to get this object * Suitable for single thread : Not safe under multithreading
public static Singleton4 getInstance(){ if(instance == null){ instance = new Singleton4(); } return instance; }
Second, the second way
public class Singleton5 {
private static Singleton5 instance; private Singleton5(){ } public static Singleton5 getInstance(){ if(instance == null){ //后面如果new过的话,就先判断一下 synchronized (Singleton5.class){//同步 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if(instance == null){ instance = new Singleton5(); } } } return instance; } }
Three, the third way
Static inner class, no thread safety issues
public class Singleton4{
private Singleton4(){ } private class inner{ private static final Singleton4 INSTANCE = new Singleton4(); } public static singleton4 getinstance(){ return inner.INSTANCE } }
Recommended: Shanghai vi design