饿汉式:
package com.ajax.pattern.singleton; import lombok.AccessLevel; import lombok.AllArgsConstructor; @AllArgsConstructor(access = AccessLevel.PRIVATE) public class Singleton { private static Singleton singleton = new Singleton(); public static Singleton getInstance() { return singleton; } }
懒汉式:
package com.ajax.pattern.singleton; import lombok.AccessLevel; import lombok.AllArgsConstructor; @AllArgsConstructor(access = AccessLevel.PRIVATE) public class SingletonEx { private static SingletonEx instance = null; public static SingletonEx getInstance() { if (instance == null) { synchronized (SingletonEx.class) { if (instance == null) { instance = new SingletonEx(); } } } return instance; } }
枚举:——
原文地址:https://www.cnblogs.com/caizhiqin/p/11290607.html
时间: 2024-11-02 03:38:28