Android 中两种单例的写法
2019-04-08 10:08
225 查看
版权声明:转载请注明出处。 https://blog.csdn.net/qq_43183022/article/details/89082391
个人常用的有 DCL 和静态内部类实现的单例。
分别记录一下。
DCL
public class DataHelper { private static volatile DataHelper instance; private DataHelper() { } public static DataHelper getDefault() { if (instance == null) { synchronized (DataHelper.class) { if (instance == null) { instance = new DataHelper(); } } } return instance; } }
静态内部类方式
public class DataHelper { private DataHelper() { } public static DataHelper getInstance() { return SingletonHolder.instance; } private static class SingletonHolder { private static DataHelper instance = new DataHelper(); } }
相关文章推荐
- Android发送post请求的两种写法(okHttp&&httpClient)
- android中状态机原理之状态机的两种写法
- Android弹出popupWindow 背景变暗(背景半透明)(两种写法)
- Android网络访问Post请求的两种写法
- Android 按钮事件两种经典写法
- Android中常用的两种动画写法
- android中单例的最佳两种写法
- Android底部导航栏最常用的两种写法
- 【Android】两种抽屉写法(NavigationView和Toolbar互动)
- android onClick()方法的两种写法
- Android 工具类的两种写法---单例模式与静态方法
- Android 两种隐藏系统状态栏的写法的区别
- 8.1、Android中为Intent附加数据的两种写法
- Android remount命令的两种写法
- 快速排序partition过程常见的两种写法+快速排序非递归实现
- android应用打包成apk的两种方式
- Mule ESB java组件两种写法
- android 将int转byte,byte转int的两种方法
- android animation的两种应用实例
- android 发送短信的两种方式