程序员,你只能有一个媳妇儿!

【威哥说】今天是10月10日了,上班第3天,大家应该已经调整好状态了吧!既然选择了前方便风雨兼程,不要有一丝的松懈。将来的你一定会感谢现在努力拼命的自己。【也许你的朋友正在等你转发到朋友圈】

【正文】

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

单例模式可以说是所有模式中最简单的一种,它自始至终只能创建一个实例,可以有两种形式,分别为懒汉式和饿汉式。

饿汉式,很简单,一开始就创建了实例,实际上到底会不会被调用也不管

/**

* 饿汉式,线程安全

*

* @author 才子

*

*/

public class SingletonHungry {

private static SingletonHungry instance = new SingletonHungry();

private SingletonHungry() {

}

public static SingletonHungry getInstance() {

return instance;

}

}

懒汉式,由于是线程不安全的,在多线程中处理会有问题,所以需要加同步

/**

* 懒汉式,这是线程不安全的,如果有多个线程在执行,有可能会创建多个实例

*

* @author 才子

*

*/

public class SingletonIdler {

private static SingletonIdler instance = null;

private SingletonIdler() {

}

public static SingletonIdler getInstance() {

if (instance == null) {

instance = new SingletonIdler();

}

return instance;

}

}

加了同步之后的代码,每次进来都要判断下同步锁,比较费时,还可以进行改进

/**

* 懒汉式

*

* @author 才子

*

*/

public class SingletonIdler {

private static SingletonIdler instance = null;

private SingletonIdler() {

}

public synchronized static SingletonIdler getInstance() {

if (instance == null) {

instance = new SingletonIdler();

}

return instance;

}

}

加同步代码块,只会判断一次同步,如果已经创建了实例就不会判断,减少了时间

/**

* 懒汉式

*

* @author 才子

*

*/

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;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 【威哥说】今天是10月10日了,上班第3天,大家应该已经调整好状态了吧!既然选择了前方便风雨兼程,不要有一丝的松懈...
    威哥爱编程阅读 2,312评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,084评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,166评论 18 399
  • 每个人都有自己珍惜,想为之努力奋斗的东西。 有的是因为梦想,有的是因为无奈,有的是因为一些人一些事,或许这些东西在...
    Mr绍君阅读 3,550评论 1 1
  • 今天听了几节萧秋水的《学习知识管理,优秀到不能被忽视》,对与文字的写作有了几点感受。 1、输入与输出 写作100天...
    温妮小世界阅读 1,495评论 0 0

友情链接更多精彩内容