Android设计模式 单例模式

版权声明:本文为博主原创文章,未经博主允许不得转载。http://blog.csdn.net/capeng https://blog.csdn.net/capeng/article/details/53507464

  单例有比较多的实现方法。什么懒汉,什么恶汉。什么DCL的。其实,就算你懂这些,也并没有什么卵用的。。。。

对于单例只要知道两件事情:

1、单例是为了确保一个类只有一个实例,只被实例化一次且能想整个app提供这个实例。你要你能实现这个思想的都叫单例.你可能需要考虑线程安全,

多余同步、资源消耗。

2、就是单例的两种写法:


a、静态内部类:

 public class Singleton{

private Singleton(){}

public static Singleton getInstance(){

return SingletonHolder.sInstance;

}

private static class SingletonHolder{

private static final Singleton sInstance = new Singleton();

}

}


使用:Singleton.getInstance.xxxxxxxx;


b、枚举单例:



使用的时候  :Singleton.Instance.doSomeThing();


当然,这只是单例的一小部分内容。。。。还有更多的。。。。去找谷哥。度娘也有。只不过千篇一律。。。也没有更多深入讨论的东西




猜你喜欢

转载自blog.csdn.net/capeng/article/details/53507464