单例模式
单例模式的定义:即无论何种情况下只能又一个唯一的实例。
优点:
1.减少内存的开支,当一个对象被频繁的创建和销毁,这个时候使用单例效果就很明显。
2.提高性能,当创建一个对象需要访问很多资源的时候,比如,读配置,加载文件...
3.单例模式可以避免对资源的多重占用,当一个
4.提供全局访问点,优化共享资源的访问。
缺点:
因为单例是没有接口的,所以扩展起来就必须修改源代码。
1.饿汉式单例
类加载的时候就已经创建了实例
优点 :提升效率
缺点:占用内存
public class HungrySingleton {
private static final HungrySingleton instance;
static {
instance = new HungrySingleton();
}
private HungrySingleton() {
}
public static HungrySingleton getInstance() {
return instance;
}
}
2.懒汉式单例
第一次调用的时候创建实例。
优点:节省内存,只有使用到实例的时候才会创建
缺点:反射可以破坏单例,线程不安全(简单的懒汉式)
public class LazySingleton {
private volatile static LazySingleton instance;
private LazySingleton() {
}
public static LazySingleton getInstance() {
if (null == instance) {
synchronized (LazySingleton.class) {
if (null == instance) {
instance = new LazySingleton();
}
}
}
return instance;
}
}
3.容器式单例
容器式单例 解决了大批量的创建单例
缺点:线程不安全,序列化破坏单例
public class ContainerSingleton {
private ContainerSingleton() {
}
private static Map<String, Object> ioc = new ConcurrentHashMap<>();
public static Object getInstance(String className) {
try {
if (!ioc.containsKey(className)) {
Object instance = Class.forName(className).newInstance();
return instance;
}
} catch (Exception e) {
e.printStackTrace();
}
return ioc.get(className);
}
}
4.序列化单例
防止序列化破坏单例,加入 readResolve() 方法
public class SeriableSingleton {
private SeriableSingleton() {
}
private static final SeriableSingleton instance = new SeriableSingleton();
public static SeriableSingleton getInstance() {
return instance;
}
private Object readresolve() {
return instance;
}
}
5.ThreadLoca单例
自带线程安全,因为每一个线程独享自己的threadLocal
public class ThreadLocalSingleton {
private ThreadLocalSingleton() {
}
private static ThreadLocal<Object> thread;
static {
thread = new ThreadLocal<Object>() {
@Override
protected Object initialValue() {
return new ThreadLocalSingleton();
}
};
}
public static ThreadLocalSingleton getInstance() {
return (ThreadLocalSingleton) thread.get();
}
}