博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android设计模式——单例模式。
阅读量:3701 次
发布时间:2019-05-21

本文共 2723 字,大约阅读时间需要 9 分钟。

分享最常用的单例模式。

饿汉式——简单粗暴在初始化的时候就加载。

package com.example.visualizerview;/** * 饿汉式 * @author Zxt * */public class Singleton {        private static Singleton instance = new Singleton();        private Singleton()    {            }        public static Singleton getInstance()    {        return instance;    }}
在初始化速度非常快,占用内存较小时适用,不会出现线程安全问题。

懒汉式——延迟加载。

package com.example.visualizerview;/** * 懒汉式 * @author Zxt * */public class Singleton {		public static Singleton instance = null;	private Singleton()	{			}		public static Singleton getInstance()	{		if(instance == null)		{			instance = new Singleton();		}		return instance;	}}
在需要的时候才进行加载。

多线程:

上面介绍的两种单例模式都是在最基本常规(单线程)的情况下的写法。

多线程的情况下:

饿汉式不会出现问题应为JVM只加载一次单例类。

懒汉式则可能出现重复创建的单例对象的情况原因如下:

1,常见的解决办法:

或:

public class Singleton {		public static Singleton instance = null;	private Singleton()	{			}		public static synchronized Singleton getInstance()	{		if(instance == null)		{			instance = new Singleton();		}		return instance;	}}
这种写法每次调用getInstance()时都进行同步,造成不必要的开销。

2,提高性能的写法:

3,静态内部类写法——线程安全:

4,枚举单例:

/** * 枚举单例写法 * @author Zxt * */public enum Singleton {		instance;		public void doSomething()	{			}}
使用枚举单例:

@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);						Singleton instance = Singleton.instance;		instance.doSomething();}

实际使用:

上面说的内部类和枚举的写法实际中用的其实不多,下面上一段我项目中用的单例写法:

public class SleepController {    private static final String TAG = "SleepController";    private static Context mContext;    private ManagerService mManagerService;    private MusicService.State mPlayState;      private Handler mHandler;    private HandlerThread mThread;    private TimeReminderReceiver timeReminderReceiver;    private static SleepController mInstance;    public SleepController(Context applicationContext,ManagerService mManagerService) {        mContext = applicationContext;        this.mManagerService = mManagerService;        mThread = new HandlerThread("background");        mThread.start();        mHandler = new Handler(mThread.getLooper());        //定时提醒        timeReminderReceiver = new TimeReminderReceiver();        IntentFilter timeReminderFilter = new IntentFilter();        timeReminderFilter.addAction(GlobalConstant.BROADCAST_TIME_REMINDER);        mContext.registerReceiver(timeReminderReceiver, timeReminderFilter);    }    public static SleepController getInstances(Context context,ManagerService mManagerService) {        if (mInstance == null) {            synchronized (SleepController.class) {                if (mInstance == null) {                    mInstance = new SleepController(context.getApplicationContext(),mManagerService);                }            }        }        return mInstance;    }

你可能感兴趣的文章
Redis 的 Java 客户端
查看>>
Redis 做分布式锁
查看>>
Redis 做消息队列
查看>>
Linux 的网络配置
查看>>
Linux 环境下安装 MySQL,各种踩坑、疑难杂症
查看>>
MySQL性能【索引优化分析】
查看>>
MySQL性能【查询截取分析】
查看>>
MySQL 锁机制
查看>>
3. Shiro 授权
查看>>
Git 版本管理
查看>>
MyBatis Plus 3.X 通俗易懂版教程
查看>>
Java 中一个元素在集合中如何忽略自己循环比对是否存在相同元素
查看>>
JAVA如何计算字符串公式
查看>>
Java 实现 crc modbus 16 位校验算法
查看>>
Java工作中常用方法总结
查看>>
vue 项目修改 title 旁边的 icon 图片
查看>>
Jeecg-Boot前端图片更改(网站 title 图标修改)
查看>>
前端通过 v-html 或 js 都可以替换富文本中的标签
查看>>
自定义注解通过反射实现Excel的导出功能(提供项目源码)
查看>>
SpringBoot + Ant Design Vue 实现 excel 导入功能
查看>>