Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android與設計模式——代理(Proxy)模式

Android與設計模式——代理(Proxy)模式

編輯:關於Android編程

在閻宏博士的《JAVA與模式》一書中開頭是這樣描述代理(Proxy)模式的:

  代理模式是對象的結構模式。代理模式給某一個對象提供一個代理對象,並由代理對象控制對原對象的引用。

代理模式的結構。

  所謂代理,就是一個人或者機構代表另一個人或者機構采取行動。在一些情況下,一個客戶不想或者不能夠直接引用一個對象,而代理對象可以在客戶端和目標對象之間起到中介的作用。

  代理模式類圖如下:

\

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+oaGhodTatPrA7cSjyr3W0LXEvcfJq6O6PC9wPgo8cD6hoaGhofE8c3Ryb25nPrPpz/O21M/zvcfJq6OoUGhvbmWjqaO6PC9zdHJvbmc+yfnD98HLxL+x6rbUz/O6zbT6wO221M/ztcS5ss2svdO/2qOs1eLR+dK7wLTU2sjOus6/ydLUyrnTw8S/seq21M/ztcS12Le9try/ydLUyrnTw7T6wO221M/zoaM8L3A+CjxwPqGhoaGh8TxzdHJvbmc+xL+x6rbUz/O9x8mro6hQaG9uZUJhc2WjqaO6PC9zdHJvbmc+tqjS5cHLtPrA7bbUz/PL+bT6se21xMS/seq21M/zoaM8L3A+CjxwPqGhoaGh8TxzdHJvbmc+tPrA7bbUz/O9x8mro6hQaG9uZVByb3h5o6mjujwvc3Ryb25nPrT6wO221M/zxNqyv7qs09DEv7HqttTP87XE0v3Tw6OstNO2+L/J0tTU2sjOus7KsbrystnX98S/seq21M/zo7u0+sDtttTP88zhuanSu7j20+vEv7HqttTP88/gzay1xL3Tv9qjrNLUseO/ydLU1NrIzrrOyrG68szmtPrEv7HqttTP86GjtPrA7bbUz/PNqLOj1Nq/zbuntsu199PDtKu13bj4xL+x6rbUz/PWrsewu/LWrrrzo6zWtNDQxLO49rLZ1/ejrLb4srvKx7WltL+12L2rtffTw7Srtd24+MS/seq21M/zoaM8L3A+CiDJz828tcS0+sDtxKPKvc28yrnTw7XEysdBbmRyb2lkIFBob25ludzA7bXEwP3X06OstNPNvNbQv8nS1L+0tb2jrNauy/nS1NKqyrnTw7T6wO3Eo8q9o6y+zcrHzqrBy7ncwO2yu82swODQzbXEUGhvbmWjrLfDzsrV37K70OjSqtaqtcBBbmRyb2lkz7XNs8/r0qrKssO0wODQzbXEUGhvbmWjrNaxvdPKudPDUGhvbmVQcm94ebbUz/O+zb/JoaPPwsPmv7S/tMv8tcS087jFyrXP1joKPHA+PHN0cm9uZz6z6c/zttTP873HyasoUGhvbmUpOjwvc3Ryb25nPjwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">public interface Phone { ..... /** * Get the current ServiceState. Use * registerForServiceStateChanged to be informed of * updates. */ ServiceState getServiceState(); /** * Get the current CellLocation. */ CellLocation getCellLocation(); /** * @return all available cell information or null if none. */ public List getAllCellInfo(); ...... }
目標對象角色(PhoneBase(PhoneBase的具體實現體現在其子類中,以GSMPhone為例)):

public class GSMPhone extends PhoneBase {
    ......
        @Override
    public ServiceState
    getServiceState() {
        if (mSST != null) {
            return mSST.mSS;
        } else {
            // avoid potential NPE in EmergencyCallHelper during Phone switch
            return new ServiceState();
        }
    }

    @Override
    public CellLocation getCellLocation() {
        return mSST.getCellLocation();
    }

    @Override
    public PhoneConstants.State getState() {
        return mCT.mState;
    }

    @Override
    public int getPhoneType() {
        return PhoneConstants.PHONE_TYPE_GSM;
    }

    ......


}

代理對象角色(PhoneProxy):

public class PhoneProxy extends Handler implements Phone {
    ......
    private Phone mActivePhone;
    private CommandsInterface mCommandsInterface;
    private IccSmsInterfaceManager mIccSmsInterfaceManager;
    private IccPhoneBookInterfaceManagerProxy mIccPhoneBookInterfaceManagerProxy;
    private PhoneSubInfoProxy mPhoneSubInfoProxy;
    private IccCardProxy mIccCardProxy;
    ......
    public PhoneProxy(PhoneBase phone) {
        ......
        mActivePhone = phone; //具體對象傳進來了
        ......
        mCommandsInterface = ((PhoneBase)mActivePhone).mCi; //使用具體對象(這裡是GSMPhone的對象)的對象
        .......
    }

    @Override
    public ServiceState getServiceState() {
        return mActivePhone.getServiceState(); //調用具體對象(這裡是GSMPhone的對象)的方法完成功能
    }
@Override public CellLocation getCellLocation() { return mActivePhone.getCellLocation(); } /** * @return all available cell information or null if none. */ @Override public List getAllCellInfo() { return mActivePhone.getAllCellInfo(); } /** * {@inheritDoc} */ @Override public void setCellInfoListRate(int rateInMillis) { mActivePhone.setCellInfoListRate(rateInMillis); } .....}


我們看看客戶端是怎麼使用的:

public class PhoneFactory {
    static private Phone sProxyPhone = null;
    ......
    public static void makeDefaultPhone(Context context) {
    ......
                int phoneType = TelephonyManager.getPhoneType(networkMode);
                if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
                    Log.i(LOG_TAG, "Creating GSMPhone");
                    sProxyPhone = new PhoneProxy(new GSMPhone(context,
                            sCommandsInterface, sPhoneNotifier));//我要制造GSM手機
                } else if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
                    switch (TelephonyManager.getLteOnCdmaModeStatic()) {
                        case PhoneConstants.LTE_ON_CDMA_TRUE:
                            Log.i(LOG_TAG, "Creating CDMALTEPhone");
                            sProxyPhone = new PhoneProxy(new CDMALTEPhone(context,
                                sCommandsInterface, sPhoneNotifier));//我要制造4G CDMA手機
                            break;
                        case PhoneConstants.LTE_ON_CDMA_FALSE:
                        default:
                            Log.i(LOG_TAG, "Creating CDMAPhone");
                            sProxyPhone = new PhoneProxy(new CDMAPhone(context,
                                    sCommandsInterface, sPhoneNotifier));//我要制造3G CDMA手機
                            break;
                    }
                }
    ......
}
    ......
}

未完待續,有不對的地方,請指正。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved