提交 87697d97 编写于 作者: 武汉红喜's avatar 武汉红喜

Singleton

上级 ce03f5cd
package org.hongxi.java.util.lang;
package org.hongxi.java.util.lang.oop;
/**
* @author shenhongxi 2019/8/11
......
package org.hongxi.java.util.lang;
package org.hongxi.java.util.lang.oop;
/**
* @author shenhongxi 2019/8/11
......
package org.hongxi.java.util.lang.singleton;
/**
* @author shenhongxi 2019/8/11
*/
public class EagerSingleton {
private static EagerSingleton instance = new EagerSingleton();
private EagerSingleton() {}
public static EagerSingleton getInstance() {
return instance;
}
}
package org.hongxi.java.util.lang.singleton;
/**
* @author shenhongxi 2019/8/11
*/
public enum EnumSingleton {
INSTANCE;
public String config;
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
}
package org.hongxi.java.util.lang.singleton;
/**
* @author shenhongxi 2019/8/11
*/
public class InnerClassSingleton {
private InnerClassSingleton() {}
private static class SingletonHolder {
private static final InnerClassSingleton instance = new InnerClassSingleton();
}
public static InnerClassSingleton getInstance() {
return SingletonHolder.instance;
}
}
package org.hongxi.java.util.lang.singleton;
/**
* @author shenhongxi 2019/8/11
*/
public class LazySingleton {
private volatile static LazySingleton instance;
private LazySingleton() {}
public static LazySingleton getInstance() {
if (instance == null) {
synchronized (LazySingleton.class) {
if (instance == null) {
instance = new LazySingleton();
}
}
}
return instance;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册