編輯:關於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
目標對象角色(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 Listpublic 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;
}
}
......
}
......
}
未完待續,有不對的地方,請指正。
開發隨筆,小結項目開發中的得與失,項目優化工作,用到了以下幾個知識點,在這裡和大家分享一下:進展-界面、推薦邏輯優化:layout_margin、layout_heigh
背景Android是基於Linux的操作系統,在其中運行的應用或者系統服務,實際上就是一個個Linux進程。這意味著它們彼此之間是隔離的,必須通過進程間通信(IPC)來相
1.相關屬性:下面是GridView中的一些屬性: android:columnWidth:設置列的寬度 android:gravity:組件對其方式 and
u-boot對環境變量的處理主要包括兩部分:一是環境變量初始化,二是環境變量的設定、刪除等操作。下面將分別進行討論。這裡所使用的u-boot版本為2015.7,硬件為I.