模式設(shè)計(jì)c#--行為型--state
| 名稱 | State |
| 結(jié)構(gòu) |
|
| 意圖 | 允許一個(gè)對(duì)象在其內(nèi)部狀態(tài)改變時(shí)改變它的行為。對(duì)象看起來似乎修改了它的類。 |
| 適用性 |
|
namespace
?State_DesignPattern
{
????
using
?System;
????
abstract
?
class
?State?
????
{
????????
protected
?
string
?strStatename;????????
????????
abstract
?
public
?
void
?Pour();
????????
//
?do?something?state-specific?here
????}
????
class
?OpenedState?:?State?
????
{????????
????????
public
?OpenedState?()
????????
{
????????????strStatename?
=
?
"
Opened
"
;
????????}
????????
override
?
public
?
void
?Pour()
????????
{
????????????Console.WriteLine(
"
pouring
"
);
????????????Console.WriteLine(
"
pouring
"
);
????????????Console.WriteLine(
"
pouring
"
);
????????}
????}
????
????
class
?ClosedState?:?State?
????
{????????
????????
public
?ClosedState()
????????
{
????????????strStatename?
=
?
"
Closed
"
;
????????}
????????
override
?
public
?
void
?Pour()
????????
{
????????????Console.WriteLine(
"
ERROR?-?bottle?is?closed?-?cannot?pour
"
);
????????}
????}
????
class
?ContextColaBottle?
????
{
????????
public
?
enum
?BottleStateSetting?
{
????????????Closed,
????????????Opened
????????}
;
????????
//
?If?teh?state?classes?had?large?amounts?of?instance?data,
????????
//
?we?could?dynamically?create?them?as?needed?-?if?this?demo
????????
//
?they?are?tiny,?so?we?just??create?them?as?data?members
????????OpenedState?openedState?
=
?
new
?OpenedState();
????????ClosedState?closedState?
=
?
new
?ClosedState();
????????
public
?ContextColaBottle?()
????????
{
????????????
//
?Initialize?to?closed
????????????CurrentState?
=
?closedState;
????????}
????????
private
?State?CurrentState;
????????
????????
public
?
void
?SetState(BottleStateSetting?newState)
????????
{
????????????
if
?(newState?
==
?BottleStateSetting.Closed)
????????????
{
????????????????CurrentState?
=
?closedState;
????????????}
????????????
else
?
????????????
{
????????????????CurrentState?
=
?openedState;
????????????}
????????}
????????
public
?
void
?Pour()
????????
{
????????????CurrentState.Pour();
????????}
????
????}
??????
/**/
///
?
<summary>
????
///
????Summary?description?for?Client.
????
///
?
</summary>
????
public
?
class
?Client
????
{
????????
public
?
static
?
int
?Main(
string
[]?args)
????????
{
????????????ContextColaBottle?contextColaBottle?
=
?
new
?ContextColaBottle();
????????????Console.WriteLine(
"
initial?state?is?closed
"
);
????????????Console.WriteLine(
"
Now?trying?to?pour
"
);
??????????????contextColaBottle.Pour();
????????????Console.WriteLine(
"
Open?bottle
"
);
????????????contextColaBottle.SetState(ContextColaBottle.BottleStateSetting.Opened);
????????????Console.WriteLine(
"
Try?to?pour?again
"
);
????????????contextColaBottle.Pour();
????????????
return
?
0
;
????????}
????}
}
| 設(shè)計(jì)模式:關(guān)于State模式的一些需要注意的地方 |
|
這個(gè)模式使得軟件可以在不同的state下面呈現(xiàn)出完全不同的特征
這個(gè)模式使得操作的state邏輯更加的清楚,省去了無數(shù)的state判斷,而state的擴(kuò)展性和可維護(hù)性和執(zhí)行效率也大幅度的上升。關(guān)于state,有如下幾點(diǎn)要注意的地方:
對(duì)象的切換:
//
?State?pattern?--?Structural?example??
using
?System;![]()
namespace
?DoFactory.GangOfFour.State.Structural![]()
{ ?? ??
//
?MainApp?test?application?
??
class
?MainApp![]() ??
{ ????
static
?
void
?Main()![]() ????
{ ??????
//
?Setup?context?in?a?state?
??????Context?c?
=
?
new
?Context(
new
?ConcreteStateA());![]() ??????
//
?Issue?requests,?which?toggles?state?
??????c.Request(); ??????c.Request(); ??????c.Request(); ??????c.Request();![]() ??????
//
?Wait?for?user?
??????Console.Read(); ????}
??}
??
//
?"State"?
??
abstract
?
class
?State![]() ??
{ ????
public
?
abstract
?
void
?Handle(Context?context); ??}
??
//
?"ConcreteStateA"?
??
class
?ConcreteStateA?:?State![]() ??
{ ????
public
?
override
?
void
?Handle(Context?context)![]() ????
{ ??????context.State?
=
?
new
?ConcreteStateB(); ????}
??}
??
//
?"ConcreteStateB"?
??
class
?ConcreteStateB?:?State![]() ??
{ ????
public
?
override
?
void
?Handle(Context?context)![]() ????
{ ??????context.State?
=
?
new
?ConcreteStateA(); ????}
??}
??
//
?"Context"?
??
class
?Context![]() ??
{ ????
private
?State?state;![]() ????
//
?Constructor?
????
public
?Context(State?state)![]() ????
{ ??????
this
.State?
=
?state; ????}
????
//
?Property?
????
public
?State?State![]() ????
{![]() ??????
get
{?
return
?state;?}
??????
set
??????
{? ????????state?
=
?value;? ????????Console.WriteLine(
"
State:?
"
?
+
? ??????????state.GetType().Name); ??????}
????}
????
public
?
void
?Request()![]() ????
{ ??????state.Handle(
this
); ????}
??}
}
?
? |
posted on 2006-01-03 16:15 夢(mèng)在天涯 閱讀(1127) 評(píng)論(2) 編輯 收藏 引用 所屬分類: Design pattern

