最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2013年01月04日 15:35:18   作者:  
動態(tài)確保某一個類只有一個實例,而且自行實例化并向整個系統(tǒng)提供這個實例這就是Android單例模式應(yīng)用,接下來詳細(xì)介紹,有需求的朋友可以參考下
單例模式定義
Ensure a class has only one instance, and provide a global point of access to it.
動態(tài)確保某一個類只有一個實例,而且自行實例化并向整個系統(tǒng)提供這個實例。

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


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

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

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

當(dāng)沒有創(chuàng)建實例對象時,調(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類實例
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);
}
}

等等。。。
新年的第一周的開始,從最簡單的單例模式開始記錄自己的學(xué)習(xí)過程吧~~~

相關(guān)文章

  • android開發(fā)中ListView與Adapter使用要點介紹

    android開發(fā)中ListView與Adapter使用要點介紹

    項目用到ListView,由于要用到 ImageView ,圖片源不是在資源里面的,沒法使用資源 ID,因此無法直接使用SimpleAdapter,要自己寫一個Adapter。 在使用ListView和Adapter需要注意以下幾點
    2013-06-06
  • 手把手教學(xué)Android用jsoup解析html實例

    手把手教學(xué)Android用jsoup解析html實例

    本篇文章主要介紹了手把手教學(xué)Android用jsoup解析html實例,jsoup 是一款Java 的HTML解析器。具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 關(guān)于Kotlin委托你必須重視的幾個點

    關(guān)于Kotlin委托你必須重視的幾個點

    委托模式已經(jīng)被證明是實現(xiàn)繼承的一個很好的替代方式,下面這篇文章主要給大家介紹了關(guān)于Kotlin委托你必須重視的幾個點,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • Android基于OkHttp實現(xiàn)下載和上傳圖片

    Android基于OkHttp實現(xiàn)下載和上傳圖片

    這篇文章主要為大家詳細(xì)介紹了Android基于OkHttp實現(xiàn)下載和上傳圖片功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android提高之XML解析與生成實例詳解

    Android提高之XML解析與生成實例詳解

    這篇文章主要介紹了Android的XML解析與生成方法,相信對大家的Android項目開發(fā)有一定的借鑒參考作用,需要的朋友可以參考下
    2014-08-08
  • android surfaceView實現(xiàn)播放視頻功能

    android surfaceView實現(xiàn)播放視頻功能

    這篇文章主要為大家詳細(xì)介紹了android surfaceView實現(xiàn)播放視頻功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android使用gradle讀取并保存數(shù)據(jù)到BuildConfg流程詳解

    Android使用gradle讀取并保存數(shù)據(jù)到BuildConfg流程詳解

    這篇文章主要介紹了Android使用gradle從資源目錄讀取數(shù)據(jù)并存到BuildConfg內(nèi),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • monkeyrunner之安卓開發(fā)環(huán)境搭建教程(1)

    monkeyrunner之安卓開發(fā)環(huán)境搭建教程(1)

    這篇文章主要介紹了monkeyrunner之安卓開發(fā)環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android刮刮卡實現(xiàn)原理與代碼講解

    Android刮刮卡實現(xiàn)原理與代碼講解

    這篇文章主要為大家詳細(xì)介紹了Android刮刮卡實現(xiàn)原理、實現(xiàn)原理步驟以及代碼講解,感興趣的小伙伴們可以參考一下
    2016-04-04
  • Android自定義控件樣式實例詳解

    Android自定義控件樣式實例詳解

    這篇文章主要介紹了Android自定義控件樣式的方法,結(jié)合實例形式分析說明了常見樣式的含義與使用方法,需要的朋友可以參考下
    2016-01-01

最新評論

卫辉市| 南昌市| 元江| 正宁县| 泗水县| 芜湖市| 东兰县| 光泽县| 福贡县| 平果县| 那坡县| 南乐县| 遂平县| 黄梅县| 宁阳县| 永平县| 永定县| 沂水县| 那曲县| 定襄县| 达拉特旗| 扶风县| 辽源市| 锡林郭勒盟| 邵东县| 惠东县| 同德县| 塘沽区| 临夏市| 富川| 武陟县| 揭东县| 铜川市| 苗栗市| 永登县| 罗定市| 梅州市| 黔南| 永清县| 大庆市| 闻喜县|