单例模式

单例模式

单例模式的定义:即无论何种情况下只能又一个唯一的实例。
优点:
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();

    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值