麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 系統(tǒng) > Android > 正文

Android源碼學(xué)習(xí)之單例模式應(yīng)用及優(yōu)點(diǎn)介紹

2020-04-11 12:36:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
單例模式定義
Ensure a class has only one instance, and provide a global point of access to it.
動(dòng)態(tài)確保某一個(gè)類只有一個(gè)實(shí)例,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例。

    如上圖所示(截取自《Head First Design Patterns》一書)。


通過(guò)使用private的構(gòu)造函數(shù)確保了在一個(gè)應(yīng)用中產(chǎn)生一個(gè)實(shí)例,并且是自行實(shí)例化(在Singleton中自己使用new Singleton())。
具體單例模式有什么優(yōu)點(diǎn)呢
由于單例模式在內(nèi)存中只有一個(gè)實(shí)例,減少了內(nèi)存開銷。
單例模式可以避免對(duì)資源的多重占用,例如一個(gè)寫文件時(shí),由于只有一個(gè)實(shí)例存在內(nèi)存中,避免對(duì)同一個(gè)資源文件的同時(shí)寫操作。
單例模式可以再系統(tǒng)設(shè)置全局的訪問(wèn)點(diǎn),優(yōu)化和共享資源訪問(wèn)。
其中使用到單例模式時(shí),考慮較多的就是多線程的情況下如何防止被多線程同時(shí)創(chuàng)建等問(wèn)題,其中《Head First Design Patterns》使用到“double-checked locking”來(lái)降低使用synchronization。
復(fù)制代碼 代碼如下:

public class Singleton {
/* The volatile keyword ensures that multiple threads
* handle the uniqueInstance variable correctly when it
* is being initialized to the Singleton instance.
* */
private volatile static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if(uniqueInstance == null) {
synchronized (Singleton.class) {
if(uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}

在Android源碼中,使用到單例模式的例子很多,如:
一、 如InputMethodManager類
復(fù)制代碼 代碼如下:

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;

創(chuàng)建唯一的實(shí)例static InputMethodManager mInstance;
復(fù)制代碼 代碼如下:

/**
* Retrieve the global InputMethodManager instance, creating it if it
* doesn't already exist.
* @hide
*/
static public InputMethodManager getInstance(Context context) {
return getInstance(context.getMainLooper());
}
/**
* Internally, the input method manager can't be context-dependent, so
* we have this here for the places that need it.
* @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;
}

防止多線程同時(shí)創(chuàng)建實(shí)例
復(fù)制代碼 代碼如下:

synchronized (mInstanceSync) {
if (mInstance != null) {
return mInstance;
}

當(dāng)沒(méi)有創(chuàng)建實(shí)例對(duì)象時(shí),調(diào)用mInstance = new InputMethodManager(service, mainLooper);
其中類構(gòu)造函數(shù)如下所示:
復(fù)制代碼 代碼如下:

InputMethodManager(IInputMethodManager service, Looper looper) {
mService = service;
mMainLooper = looper;
mH = new H(looper);
mIInputContext = new ControlledInputConnectionWrapper(looper,
mDummyInputConnection);
if (mInstance == null) {
mInstance = this;
}
}

二、BluetoothOppManager類
復(fù)制代碼 代碼如下:

public class BluetoothOppManager {
private static final String TAG = "BluetoothOppManager";
private static final boolean V = Constants.VERBOSE;
// 創(chuàng)建private static類實(shí)例
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;
}
}

三、AccessibilityManager類
復(fù)制代碼 代碼如下:

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);
}
}

等等。。。
新年的第一周的開始,從最簡(jiǎn)單的單例模式開始記錄自己的學(xué)習(xí)過(guò)程吧~~~
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美亚州 | 欧美成人国产va精品日本一级 | 久久久中 | 日本最新免费二区三区 | 欧美三级日本三级少妇99 | 水卜樱一区二区av | 久久蜜桃精品一区二区三区综合网 | 在线视频观看国产 | 国产外围在线 | 亚洲欧美国产高清va在线播放 | 毛片在线视频观看 | 精品国产一区二区三区在线观看 | 九九福利视频 | 亚洲第一色婷婷 | 日本一区二区不卡高清 | 石原莉奈日韩一区二区三区 | 国产瑟瑟视频 | 亚洲性综合网 | 国产精品9191 | 婷婷久久综合九色综合色多多蜜臀 | 国产三级a三级三级 | 亚洲第一成人在线 | av在线一区二区三区四区 | 欧美成人精品一区二区男人小说 | 久久久久久久国产视频 | 国产成人精品网站 | 国产一区精品在线观看 | 日韩午夜一区二区三区 | 精品国产一区二区久久 | 69性欧美高清影院 | 激情小视频在线观看 | 久久人人做 | 亚洲欧洲日产v特级毛片 | 青草视频在线观看视频 | 最新黄色电影网站 | www.777含羞草 | 国产在线播放一区二区 | 国产一级一级 | 看免费黄色大片 | 国产毛片网站 | 狠狠干狠狠操 |