Android源碼學習之單例模式應用及優點介紹

來源:互聯網
上載者:User

單例模式定義
Ensure a class has only one instance, and provide a global point of access to it.
動態確保某一個類只有一個執行個體,而且自行執行個體化並向整個系統提供這個執行個體。

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

通過使用private的建構函式確保了在一個應用中產生一個執行個體,並且是自行執行個體化(在Singleton中自己使用new Singleton())。
具體單例模式有什麼優點呢
由於單例模式在記憶體中只有一個執行個體,減少了記憶體開銷。
單例模式可以避免對資源的多重佔用,例如一個寫檔案時,由於只有一個執行個體存在記憶體中,避免對同一個資源檔的同時寫操作。
單例模式可以再系統設定全域的訪問點,最佳化和共用資源訪問。
其中使用到單例模式時,考慮較多的就是多線程的情況下如何防止被多線程同時建立等問題,其中《Head First Design Patterns》使用到“double-checked locking”來降低使用synchronization。 複製代碼 代碼如下: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類 複製代碼 代碼如下: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;

建立唯一的執行個體static InputMethodManager mInstance; 複製代碼 代碼如下:/**
* 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;
}

防止多線程同時建立執行個體複製代碼 代碼如下:synchronized (mInstanceSync) {
if (mInstance != null) {
return mInstance;
}

當沒有建立執行個體對象時,調用mInstance = new InputMethodManager(service, mainLooper);
其中類建構函式如下所示: 複製代碼 代碼如下:InputMethodManager(IInputMethodManager service, Looper looper) {
mService = service;
mMainLooper = looper;
mH = new H(looper);
mIInputContext = new ControlledInputConnectionWrapper(looper,
mDummyInputConnection);
if (mInstance == null) {
mInstance = this;
}
}

二、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;
}
}

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

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.