设计模式是前人在开发过程中总结的一些经验,我们在开发过程中依据实际的情况,套用合适的设计模式,能够使程序结构更加简单。利于程序的扩展和维护。但也不是没有使用设计模式的程序就不好。如简单的程序就不用了,有种画蛇添足的感觉。

单例模式能够说是全部模式中最简单的一种,它自始至终仅仅能创建一个实例,能够有两种形式,分别为懒汉式和饿汉式

一、饿汉式。非常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 设计模式之单例模式的更多相关文章

  1. Android 设计模式 之 单例模式

    http://blog.csdn.net/fangchongbory/article/details/7734199   目录(?)[+] 单例模式常见情景 首先实现1中的单例模式A 实现2中单例模式 ...

  2. Android设计模式系列-单例模式

    单例模式,可以说是GOF的23种设计模式中最简单的一个. 这个模式相对于其他几个模式比较独立,它只负责控制自己的实例化数量单一(而不是考虑为用户产生什么样的实例),很有意思,是一个感觉上很干净的模式, ...

  3. Android设计模式之单例模式的七种写法

    一 单例模式介绍及它的使用场景 单例模式是应用最广的模式,也是我最先知道的一种设计模式.在深入了解单例模式之前.每当遇到如:getInstance()这样的创建实例的代码时,我都会把它当做一种单例模式 ...

  4. Android设计模式之单例模式

    定义 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例 . 单例模式是设计模式中最简单的形式之一.这一模式的目的是使得类的一 ...

  5. Android设计模式(1)----单例模式

    在非常多设计模式中.我相信大多数程序员最早接触的设计模式就是单例模式啦,当然了我也不例外. 单例模式应用起来应该是全部设计模式中最简单的.单例模式尽管简单,可是假设你去深深探究单例模式,会涉及到非常多 ...

  6. Android设计模式系列

    http://www.cnblogs.com/qianxudetianxia/category/312863.html Android设计模式系列(12)--SDK源码之生成器模式(建造者模式) 摘要 ...

  7. 经常使用的android设计模式

    一般来说,经常使用的android设计模式有下面8种:单例.工厂.观察者.代理.命令.适配器.合成.訪问者.   单例模式:目的是为了让系统中仅仅有一个调用对象,缺点是单例使其它程序过分依赖它,并且不 ...

  8. 设计模式之单例模式(Singleton)

    设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...

  9. GJM : C#设计模式(1)——单例模式

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

随机推荐

  1. 【Luogu】P2536病毒检测(Trie上DP)

    题目链接 这道题我写了个01DP,f[i][j]表示跑到Trie上第i个节点,匹配到字符串第j位行不行 然后重点在*号无限匹配怎么处理 经过一番脑洞我们可以发现*号无限匹配可以拆成两种情况: 1:匹配 ...

  2. hdu 1250 树形DP

    Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  3. 标准C程序设计七---117

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  4. HYSBZ 1026: windy数(数位DP)

    类型:数位DP题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数.问[A,B]之间windy数的个数.(1 <= A <= B <= 2000000000 ) 思路 ...

  5. Delphi 从PaintBox拷贝一部分内容到TBitmap

    将指定的TPaintBox内容(假如为paintbox1)拷贝到一个TBitmap(如Bitmap),可以这么做 Bitmap.Width := PaintBox1.Width; Bitmap.Hei ...

  6. UICollectionView的cell创建直接从第三个数据开始问题

    实现的效果是这样 大概意思就是第一组没有数据就直接将改组的cell高度变成0效果实现了,但是第二组数据创建cell就出问题了--奇葩问题 * 代码问题在这```-(CGSize)collectionV ...

  7. 微信小程序踩坑之一【weui-wxss-master单选按钮图标修改思路】

    小程序原生所带的weui框架做小程序UI实在太方便了,但是他的一些细微变化也是让开发中碰到不少头疼的问题 一直以来单选多选的美化都是设计师重点表达的地方之一 而weui-wxss-master中的单选 ...

  8. 最简单的window下使用Jenkins来做自动化部署的教程

    今天我们来说一下,如何使用Jenkins+powershell脚本,将我们的.NET CORE的脚本部署到对应的服务器上. 这里我们使用的源码管理工具是TFS.虽然源码管理器比较老旧,但是原理都差不多 ...

  9. Xamarin.Forms XAML控件的公共属性

    Xamarin.Forms XAML控件的公共属性   Xamarin.Forms XAML控件有很多.通过官网API,可以查看每个控件的属性.但是官网只给出了控件的特有属性,而公共属性没有列出.所以 ...

  10. CDOJ 3 BiliBili, ACFun… And More! 模拟

    原题链接:http://acm.uestc.edu.cn/#/problem/show/3 题意: 有个人在看B站视频时有个习惯,就是每当卡住的时候,他总再次从头开始看.另外,他在看视频时会先等待T的 ...