Android设计模式--单例模式
来源:互联网 发布:淘宝怎么买二手东西 编辑:程序博客网 时间:2024/06/07 09:07
何为单例模式
所谓单例就是该类只有一个实例,并向外只提供这个实例,不提供构造方法
单例模式在android源码中的应用的例子
1.InputMethodManager
public final class InputMethodManager { static final boolean DEBUG = false; static final String TAG = "InputMethodManager"; static final Object mInstanceSync = new Object(); static InputMethodManager mInstance; final IInputMethodManager mService; final Looper mMainLooper; /** * 检索InputMethodManager的实例 instance, 如果不存在就创建 * @hide */ static public InputMethodManager getInstance(Context context) { return getInstance(context.getMainLooper()); } /** * 在内部,输入管理器不能依赖上下文,所以需要当前这个方法,然后调用上面的方法传入mainLooper * @hide */ static public InputMethodManager getInstance(Looper mainLooper) { synchronized (mInstanceSync) { if (mInstance != null) { return mInstance; } IBinder b = ServiceManager.getService(Context.INPUT_METHOD_SERVICE); IInputMethodManager service = IInputMethodManager.Stub.asInterface(b); mInstance = new InputMethodManager(service, mainLooper); } return mInstance; }
为了防止多线程同时创建多个实例(此处使用synchronized )
(synchronized (mInstanceSync) { if (mInstance != null) { return mInstance; }luetoothOppManager
2.BluetoothOppManager
public class BluetoothOppManager { private static final String TAG = "BluetoothOppManager"; private static final boolean V = Constants.VERBOSE; // 创建private static类实例 private static BluetoothOppManager INSTANCE; /** Used when obtaining a reference to the singleton instance. */ private static Object INSTANCE_LOCK = new Object();。。。 /** * Get singleton instance. */ public static BluetoothOppManager getInstance(Context context) { synchronized (INSTANCE_LOCK) { if (INSTANCE == null) { INSTANCE = new BluetoothOppManager(); } INSTANCE.init(context); return INSTANCE; } }
3.AccessibilityManager
public final class AccessibilityManager { private static final boolean DEBUG = false; private static final String LOG_TAG = "AccessibilityManager"; /** @hide */ public static final int STATE_FLAG_ACCESSIBILITY_ENABLED = 0x00000001; /** @hide */ public static final int STATE_FLAG_TOUCH_EXPLORATION_ENABLED = 0x00000002; static final Object sInstanceSync = new Object(); private static AccessibilityManager sInstance;... /** * Get an AccessibilityManager instance (create one if necessary). * * @hide */ public static AccessibilityManager getInstance(Context context) { synchronized (sInstanceSync) { if (sInstance == null) { IBinder iBinder = ServiceManager.getService(Context.ACCESSIBILITY_SERVICE); IAccessibilityManager service = IAccessibilityManager.Stub.asInterface(iBinder); sInstance = new AccessibilityManager(context, service); } } return sInstance; } /** * Create an instance. * * @param context A {@link Context}. * @param service An interface to the backing service. * * @hide */ public AccessibilityManager(Context context, IAccessibilityManager service) { mHandler = new MyHandler(context.getMainLooper()); mService = service; try { final int stateFlags = mService.addClient(mClient); setState(stateFlags); } catch (RemoteException re) { Log.e(LOG_TAG, "AccessibilityManagerService is dead", re); } }
怎么写一个单例
1.懒汉式
public static class SingleTon{ /** * volatile(不稳定的) 关键字能保证多个线程 在处理 instance的时候正确的初始化 */ private volatile static SingleTon instance; private SingleTon(){} public static SingleTon getInstance(){ if(instance == null){ synchronized (SingleTon.class){ if(instance == null){ instance = new SingleTon(); } } } return instance; } }
2.饿汉式
public static class SingleTon{ /** * volatile(不稳定的) 关键字能保证多个线程 在处理 instance的时候正确的初始化 */ private volatile static SingleTon instance; private SingleTon(){} public synchronized static SingleTon getInstance(){ instance = new SingleTon(); return instance; } }
1 0
- Android设计模式---单例
- Android单例设计模式
- Android 设计模式 之 单例模式
- Android 设计模式 之 单例模式
- Android 设计模式之单例模式
- Android 设计模式 之 单例模式
- Android设计模式系列-单例模式
- Android 设计模式 之 单例模式
- Java(Android)设计模式-单例模式
- Android设计模式--单例模式
- android设计模式之单例模式
- Android设计模式系列-单例模式
- Android 设计模式 之 单例模式
- Android 设计模式之单例模式
- Android 设计模式之单例模式
- Android 设计模式之单例模式
- Android 设计模式 之 单例模式
- android设计模式-单例模式
- Struts2复选框多文件下载
- Swift weakSelf 在闭包语法
- java中用集合编写简单的学生信息管理系统
- Verilog十大基本功3(testbench的设计 iout类型端口信号处理)
- SDN+敏捷物联,助物联网快速落地
- Android设计模式--单例模式
- 习题26 恭喜你,现在可以考试了!
- STM32 USB的DFU功能
- django 关于 DEBUG = False 影响 admin样式
- JQuery心得(持续更新中)
- [LeetCode] 58. Length of Last Word
- leetcode 326_log log10 精度问题
- 解决java 图片压缩图片图片变色问题
- 面试准备之---排序