標籤:android代理模式 proxy
在閻宏博士的《JAVA與模式》一書中開頭是這樣描述代理(Proxy)模式的:
代理模式是對象的結構模式。代理模式給某一個對象提供一個代理對象,並由代理對象控制對原對象的引用。
代理模式的結構。
所謂代理,就是一個人或者機構代表另一個人或者機構採取行動。在一些情況下,一個客戶不想或者不能夠直接引用一個對象,而代理對象可以在用戶端和目標對象之間起到中介的作用。
代理模式類圖如下:
在代理模式中的角色:
●抽象對象角色(Phone):聲明了目標對象和代理對象的共同介面,這樣一來在任何可以使用目標對象的地方都可以使用代理對象。
●目標對象角色(PhoneBase):定義了代理對象所代表的目標對象。
●代理對象角色(PhoneProxy):代理對象內部含有目標對象的引用,從而可以在任何時候操作目標對象;代理對象提供一個與目標對象相同的介面,以便可以在任何時候替代目標對象。代理對象通常在用戶端調用傳遞給目標對象之前或之後,執行某個操作,而不是單純地將調用傳遞給目標對象。
的代理模式圖使用的是Android Phone管理的例子,可以看到,之所以要使用代理模式,就是為了管理不同類型的Phone,訪問者不需要知道Android系統想要什麼類型的Phone,直接使用PhoneProxy對象就可。下面看看它的大概實現:
抽象對象角色(Phone):
public interface Phone { ..... /** * Get the current ServiceState. Use * <code>registerForServiceStateChanged</code> to be informed of * updates. */ ServiceState getServiceState(); /** * Get the current CellLocation. */ CellLocation getCellLocation(); /** * @return all available cell information or null if none. */ public List<CellInfo> 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的對象)的方法完成功能<pre name="code" class="java" style="line-height: 32px;"><span style="font-family: Arial, Helvetica, sans-serif;"> }</span> @Override public CellLocation getCellLocation() { return mActivePhone.getCellLocation(); } /** * @return all available cell information or null if none. */ @Override public List<CellInfo> 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; } } ......} ......}
未完待續,有不對的地方,請指正。
Android與設計模式——代理(Proxy)模式