标签:get 饿汉 code import scons null div lombok 饿汉式
饿汉式:
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; } }
枚举:——
标签:get 饿汉 code import scons null div lombok 饿汉式
原文地址:https://www.cnblogs.com/caizhiqin/p/11290607.html