在閻宏博士的《JAVA與模式》一書中開頭是這樣描述狀態(State)模式的:
狀態模式,又稱狀態對象模式(Pattern of Objects for States),狀態模式是對象的行為模式。
狀態模式允許一個對象在其內部狀態改變的時候改變其行為。這個對象看上去就像是改變了它的類一樣。
狀態模式的結構
用一句話來表述,
狀態模式把所研究的對象的行為包裝在不同的狀態對象裡,每一個狀態對象都屬於一個抽象狀態類的一個子類。狀態模式的意圖是讓一個對象在其內部狀態改變的時候,其行為也隨之改變。狀態模式的示意性類圖如下所示:
vcfJq6Os0rKzycnPz8LOxKO6tMvA/dbQ08lEYXRhQ29ubmVjdG9us+S1saOstqjS5b/Nu6e2y8v5uNDQy8iktcS907/ao6yyosfSsaPB9NK7uPa+38zl17TMrMDgtcTKtcD9oaPV4rj2vt/M5de0zKzA4LXEyrXA/bj4s/a0y7u3vrO21M/ztcTP1tPQ17TMrKGjPGJyPgqhoaGhofGhoaGhs+nP89e0zKwoU3RhdGUpvcfJq6O6tqjS5dK7uPa907/aSVN0YXRlo6zTw9LUt+LXsLu3vrOjqENvbnRleHSjqbbUz/O1xNK7uPbM2LaotcTXtMysy/m21NOmtcTQ0M6qoaM8YnI+CqGhoaGh8aGhoaG+38zl17TMrChDb25jcmV0ZVN0YXRlKb3HyaujurTLwP3W0NPJRGNYWFhTdGF0ZbPktbGjrMO/0ru49r7fzOXXtMyswOC2vMq1z9bBy7u3vrOjqENvbnRleHSjqbXE0ru49te0zKzL+bbU06a1xNDQzqqhozxicj4KoaGhoaHxoaGhob/Nu6e2y73HyaujurTLwP3W0NPJRGNDb250cm9sbGVys+S1saOsudzA7brNzqy7pENvbnRleHShoyjOxNfWwLTUtNPazfjC5yk8YnI+Cjxicj4KCjxwPrTLyrXA/crHQW5kcm9pZDQuNMr9vt3BrL3Tsr+31qOsRGF0YUNvbm5lY3RvbrzMs9DX1FN0YXRlTWFjaGluZde0zKy7+sDgo6xTdGF0ZU1hY2hpbmXXtMysu/rA4Mq1z9bBy1N0YXRlyei8xsSjyr21xNOm08OjrNTaV2lGaaGiwLbRwKGiyv2+3cGsvdPV4sj91tbN+MLnway907XEudzA7cq1z9a2vMq508PBy1N0YXRlyei8xsSjyr2hozwvcD4KPHA+zbzW0L+0s/ajrLmytqjS5cHLN9bW17TMrNfTwOCjrLa8ysdEYXRhQ29ubmVjdG9utcTE2rK/wOCjrMr9vt3BrL3T1NrB+dbW17TMrNbQ16q7u6OsRGVmYXVsdFN0YXRlysfX986qxuTL+8H5uPbX08DgtcS4uNe0zKzA4LXEo6zL+c69uLjXtMyswOCjrL7Nyse1sdfT17TMrMDgzt63qLSmwO2jqNTacHJvY2Vzc01lc3NhZ2W3vbeo1tC0psDto6nKsaOsvs274b27uPi4uNe0zKzA4MC0tKbA7aGj17TMrLXE16q7u8rHzai5/URhdGFDb25uZWN0b261xHRyYW5zaXRpb25Ubygpt723qMC0yejWw7XEoaM8L3A+CjxwPjxicj4KPC9wPgo8cD7UtLT6wusotPrC68yrtuCjrNa7zPlDb250ZXh0udzA7VN0YXRltcS0+sLrKaO6PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;"> private DataConnection(PhoneBase phone, String name, int id,
DcTrackerBase dct, DcTesterFailBringUpAll failBringUpAll,
DcController dcc) {
//[mr2] removed
//setDbg(false);
addState(mDefaultState);
addState(mInactiveState, mDefaultState);//mDefaultState作為父狀態
addState(mActivatingState, mDefaultState);
addState(mRetryingState, mDefaultState);
addState(mActiveState, mDefaultState);
addState(mDisconnectingState, mDefaultState);
addState(mDisconnectingErrorCreatingConnection, mDefaultState);
setInitialState(mInactiveState);//設置初始狀態
}
未完待續,有不對的地方,請指正。