Android开发中关于设计模式的总结

做了这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;
    }
}

BluetoothSettings中获取LocalBlueoothManager的实例:


                if (!isRestrictedAndNotPinProtected()) {
                    if (mDiscoverableEnabler == null) {
                        mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(),
                                mLocalAdapter, mMyDevicePreference);
                        mDiscoverableEnabler.resume();
                        LocalBluetoothManager.getInstance(getActivity()).setDiscoverableEnabler(
                                mDiscoverableEnabler);
                    }
                }
}

适配器模式
适配器模式,把一个类的接口变换成客户端所期待的另一种接口,从而使原本不匹配而无法在一起工作的两个,类能够在一起工作。
适配器模式分为类适配器模式和对象适配器模式。
关于类适配器模式,因为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();
        }
    }
}

用这条语句将observer对象注册到要监控的content上:
getContentResolver().registerContentObserver(BluetoothShare.CONTENT_URI, true, mObserver);
一旦这个content有变化的时候,observer对象的onchange方法就会被调用,用于刷新需要刷新的界面显示等内容.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值