State Mode
2016-12-20 16:52:55 2 举报
状态模式
作者其他创作
大纲/内容
SoldState
+InsertQuarter()+EjectQuarter()+TurnCrank()+Dispense()
HasQuarterState
public class NoQuarterState : State{ GumballMachine gumballMachine; public NoQuarterState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void InsertQuarter() { Console.WriteLine(\"You inserted a quarter\"); this.gumballMachine.SetState(gumballMachine.GetHasQuarterState()); } public void EjectQuarter() { Console.WriteLine(\"You haven't inserted a quarter\"); } public void TurnCrank() { Console.WriteLine(\
Dependency
Aggregation
WinnerState
NoQuarterState
GumballMachine
-noQuarterState:State-hasQuarterState:State-soldState:State-soldOutState:State-state:State = soldOutState-count:int = 0
+GumballMachine(int count)+InsertQuarter()+EjectQuarter()+TurnCrank()+SetState(State state)+ReleaseBall()
public class GumballMachine{ State noQuarterState; State hasQuarterState; State soldState; State soldOutState; State state = soldOutState; int count = 0; public GumballMachine(int count) { this.noQuarterState = new NoQuarterState(this); this.hasQuarterState = new HasQuarterState(this); this.soldState = new SoldState(this); this.soldOutState = new SoldOutState(this); this.count = count; if(this.count 0) { this.state = this.noQuarterState; } } public void InsertQuarter() { this.state.InsertQuarter(); } public void EjectQuarter() { this.state.EjectQ } public void TurnCrank() { this.state.TurnCrank(); this.state.Dispense(); } public SetState(State state) { this.state = state; } public ReleaseBall() { Console.WriteLine(\"A gumball is being sold\"); if(this.count != 0) this.count--; }}
客户端进行各种行为,其实是将行为委托给当前状态去处理
SoldOutState
State
+InsertQuerter()+EjectQuarter()+TurnCrank()+Dispense()
0 条评论
下一页