Android 设计模式之单例模式
设计模式是前人在开发过程中总结的一些经验,我们在开发过程中依据实际的情况,套用合适的设计模式,能够使程序结构更加简单。利于程序的扩展和维护。但也不是没有使用设计模式的程序就不好。如简单的程序就不用了,有种画蛇添足的感觉。
单例模式能够说是全部模式中最简单的一种,它自始至终仅仅能创建一个实例,能够有两种形式,分别为懒汉式和饿汉式
一、饿汉式。非常easy,一開始就创建了实例,实际上究竟会不会被调用也无论
package com.dzt.singleton; /**
* 饿汉式。线程安全
*
* @author Administrator
*
*/
public class SingletonHungry { private static SingletonHungry instance = new SingletonHungry(); private SingletonHungry() { } public static SingletonHungry getInstance() {
return instance;
}
}
二、懒汉式,因为是线程不安全的,在多线程中处理会有问题。所以须要加同步
package com.dzt.singleton; /**
* 懒汉式,这是线程不安全的,假设有多个线程在运行,有可能会创建多个实例
*
* @author Administrator
*
*/
public class SingletonIdler { private static SingletonIdler instance = null; private SingletonIdler() { } public static SingletonIdler getInstance() {
if (instance == null) {
instance = new SingletonIdler();
}
return instance;
}
}
加了同步之后的代码,每次进来都要推断下同步锁。比較费时。还能够进行改进
package com.dzt.singleton; /**
* 懒汉式
*
* @author Administrator
*
*/
public class SingletonIdler { private static SingletonIdler instance = null; private SingletonIdler() { } public synchronized static SingletonIdler getInstance() {
if (instance == null) {
instance = new SingletonIdler();
}
return instance;
}
}
加同步代码块,仅仅会推断一次同步,假设已经创建了实例就不会推断,降低了时间
package com.dzt.singleton; /**
* 懒汉式
*
* @author Administrator
*
*/
public class SingletonIdler { private static SingletonIdler instance = null; private SingletonIdler() { } public static SingletonIdler getInstance() {
if (instance == null) {
synchronized (SingletonIdler.class) {
if (instance == null)
instance = new SingletonIdler();
}
}
return instance;
}
}
单例模式在Androidd原生应用中也有使用,如Phone中
NotificationMgr.java类
private static NotificationMgr sInstance;
private NotificationMgr(PhoneApp app) {
mApp = app;
mContext = app;
mNotificationManager = (NotificationManager) app
.getSystemService(Context.NOTIFICATION_SERVICE);
mStatusBarManager = (StatusBarManager) app
.getSystemService(Context.STATUS_BAR_SERVICE);
mPowerManager = (PowerManager) app
.getSystemService(Context.POWER_SERVICE);
mPhone = app.phone; // TODO: better style to use mCM.getDefaultPhone()
// everywhere instead
mCM = app.mCM;
statusBarHelper = new StatusBarHelper();
}
static NotificationMgr init(PhoneApp app) {
synchronized (NotificationMgr.class) {
if (sInstance == null) {
sInstance = new NotificationMgr(app);
// Update the notifications that need to be touched at startup.
sInstance.updateNotificationsAtStartup();
} else {
Log.wtf(LOG_TAG, "init() called multiple times! sInstance = "
+ sInstance);
}
return sInstance;
}
}
Android 设计模式之单例模式的更多相关文章
- Android 设计模式 之 单例模式
http://blog.csdn.net/fangchongbory/article/details/7734199 目录(?)[+] 单例模式常见情景 首先实现1中的单例模式A 实现2中单例模式 ...
- Android设计模式系列-单例模式
单例模式,可以说是GOF的23种设计模式中最简单的一个. 这个模式相对于其他几个模式比较独立,它只负责控制自己的实例化数量单一(而不是考虑为用户产生什么样的实例),很有意思,是一个感觉上很干净的模式, ...
- Android设计模式之单例模式的七种写法
一 单例模式介绍及它的使用场景 单例模式是应用最广的模式,也是我最先知道的一种设计模式.在深入了解单例模式之前.每当遇到如:getInstance()这样的创建实例的代码时,我都会把它当做一种单例模式 ...
- Android设计模式之单例模式
定义 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例 . 单例模式是设计模式中最简单的形式之一.这一模式的目的是使得类的一 ...
- Android设计模式(1)----单例模式
在非常多设计模式中.我相信大多数程序员最早接触的设计模式就是单例模式啦,当然了我也不例外. 单例模式应用起来应该是全部设计模式中最简单的.单例模式尽管简单,可是假设你去深深探究单例模式,会涉及到非常多 ...
- Android设计模式系列
http://www.cnblogs.com/qianxudetianxia/category/312863.html Android设计模式系列(12)--SDK源码之生成器模式(建造者模式) 摘要 ...
- 经常使用的android设计模式
一般来说,经常使用的android设计模式有下面8种:单例.工厂.观察者.代理.命令.适配器.合成.訪问者. 单例模式:目的是为了让系统中仅仅有一个调用对象,缺点是单例使其它程序过分依赖它,并且不 ...
- 设计模式之单例模式(Singleton)
设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...
- GJM : C#设计模式(1)——单例模式
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
随机推荐
- 解决在IE6、7中用height来设定SELECT标签高度无效的兼容性问题
在IE6.7中用height来设定SELECT标签高度是无效的,宽度的话各浏览器设置都是一致的,解决方法就是在select外嵌套两层标签,一层用来遮挡select的默认边框(在IE6.7中设置bord ...
- HDU——1233还是畅通工程(克鲁斯卡尔+优先队列)
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- docker (centOS 7) 使用笔记3 - 修改docker默认的虚拟网址
近日在使用VPN时发现和docker的虚拟网址发生了冲突,都是172.17.0.1,故需要修改docker的默认网址. 1. 当前状态 # ifconfig docker0: flags=<UP ...
- websphere启用高速缓存导致问题
环境:websphere 7 一个流程主页,里面include了上面这个页面,内部有一个iframe: 现象:项目发布在测试环境中,打开流程主页时,里面iframe内页显示不出来: 同样的jsp页面, ...
- 数据库操作之——key与index的区别
mysql的key和index多少有点令人迷惑,这实际上考察对数据库体系结构的了解的. 1 key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引(辅助查 ...
- hdu 3264 圆的交+二分
Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu 3613 KMP算法扩展
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- La 3942 字符串+dp
题目大意:一个字符串,可以分解成若干英语单词的连接(单词可以重复使用),求有多少种方法? #include<iostream> #include<cstdio> #includ ...
- 汇编指令详解--as手册
https://sourceware.org/binutils/docs/as/ Table of Contents 1 Overview 1.1 Structure of this Manual 1 ...
- CI安全
URI安全,CodeIgniter 严格限制 URI 中所能包含的字符,以此帮助你设计的程序减少被恶意数据入侵的可能.URI 一般只包含下列内容: 字母和数字(Alpha-numeric text) ...