做了这3年多的Android系统开发,其实也涉及到了许多设计模式的应用,先简单总结一下.
单例模式
它的意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点。
Settings里面的LocalBluetoothManager就用到了单例模式:
{CSDN:CODE:public final class LocalBluetoothManager {
private static final String TAG = "LocalBluetoothManager";
/** Singleton instance. */
private static LocalBluetoothManager sInstance;
private final Context mContext;
/** If a BT-related activity is in the foreground, this will be it. */
private Context mForegroundActivity;
private BluetoothDiscoverableEnabler mDiscoverableEnabler;
private final LocalBluetoothAdapter mLocalAdapter;
private final CachedBluetoothDeviceManager mCachedDeviceManager;
/** The Bluetooth profile manager. */
private final LocalBluetoothProfileManager mProfileManager;
/** The broadcast receiver event manager. */
private final BluetoothEventManager mEventManager;
public static synchronized LocalBluetoothManager getInstance(Context context) {
if (sInstance == null) {
LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
if (adapter == null) {
return null;
}
// This will be around as long as this process is
Context appContext = context.getApplicationContext();
sInstance = new LocalBluetoothManager(adapter, appContext);
}
return sInstance;
}
private static final String TAG = "LocalBluetoothManager";
/** Singleton instance. */
private static LocalBluetoothManager sInstance;
private final Context mContext;
/** If a BT-related activity is in the foreground, this will be it. */
private Context mForegroundActivity;
private BluetoothDiscoverableEnabler mDiscoverableEnabler;
private final LocalBluetoothAdapter mLocalAdapter;
private final CachedBluetoothDeviceManager mCachedDeviceManager;
/** The Bluetooth profile manager. */
private final LocalBluetoothProfileManager mProfileManager;
/** The broadcast receiver event manager. */
private final BluetoothEventManager mEventManager;
public static synchronized LocalBluetoothManager getInstance(Context context) {
if (sInstance == null) {
LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
if (adapter == null) {
return null;
}
// This will be around as long as this process is
Context appContext = context.getApplicationContext();
sInstance = new LocalBluetoothManager(adapter, appContext);
}
return sInstance;
}
}
BluetoothSettings中获取LocalBlueoothManager的实例:
if (!isRestrictedAndNotPinProtected()) {
if (mDiscoverableEnabler == null) {
mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(),
mLocalAdapter, mMyDevicePreference);
mDiscoverableEnabler.resume();
LocalBluetoothManager.getInstance(getActivity()).setDiscoverableEnabler(
mDiscoverableEnabler);
}
}
if (mDiscoverableEnabler == null) {
mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(),
mLocalAdapter, mMyDevicePreference);
mDiscoverableEnabler.resume();
LocalBluetoothManager.getInstance(getActivity()).setDiscoverableEnabler(
mDiscoverableEnabler);
}
}
}
适配器模式
适配器模式,把一个类的接口变换成客户端所期待的另一种接口,从而使原本不匹配而无法在一起工作的两个,类能够在一起工作。
适配器模式分为类适配器模式和对象适配器模式。
关于类适配器模式,因为java的单继承,如果继承一个类,另外的则只能是接口,需要手动实现相应的方法。
适配器模式分为类适配器模式和对象适配器模式。
关于类适配器模式,因为java的单继承,如果继承一个类,另外的则只能是接口,需要手动实现相应的方法。
蓝牙共享BluetoothOpp中的传输历史列表TransferAdapter,就用到了CursorAdapter,就是适配器模式的案例
{CSDN:CODE:public class BluetoothOppTransferAdapter extends ResourceCursorAdapter }
它的作用是将cursor对应的数据源以ListView想要的目标接口的样子传给了ListView.
观察者模式
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
BluetoothShareContentObserver就是观察者模式的实例:
{CSDN:CODE:
private class BluetoothShareContentObserver extends ContentObserver {
public BluetoothShareContentObserver() {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
if (V) Log.v(TAG, "ContentObserver received notification");
updateFromProvider();
}
}
public BluetoothShareContentObserver() {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
if (V) Log.v(TAG, "ContentObserver received notification");
updateFromProvider();
}
}
}
用这条语句将observer对象注册到要监控的content上:
getContentResolver().registerContentObserver(BluetoothShare.CONTENT_URI, true, mObserver);
一旦这个content有变化的时候,observer对象的onchange方法就会被调用,用于刷新需要刷新的界面显示等内容.