单例模式(静态内部类)
import java.io.Serializable; /** * 单例模式(静态内部类) * 描述: * * @author lyn * @date 2022/3/29 18:23 */ public class Singleton implements Serializable { private static final long serialVersionUID = 1L; private Singleton() { //防止反射破坏代理模式 if (getInstance() != null) { throw new RuntimeException(); } } private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } /** * 防止序列化破坏单例模式 * * @return */ private Object readResolve() { return SingletonHolder.INSTANCE; } }