study
This commit is contained in:
53
StateMachine/CarStateMachine/Model/Car.cs
Normal file
53
StateMachine/CarStateMachine/Model/Car.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
namespace CarStateMachine.Model
|
||||
{
|
||||
public class Car
|
||||
{
|
||||
public enum State
|
||||
{
|
||||
Stopped,
|
||||
Started,
|
||||
Running,
|
||||
}
|
||||
|
||||
public enum Action
|
||||
{
|
||||
Stop,
|
||||
Start,
|
||||
Accelerate,
|
||||
}
|
||||
|
||||
private State _state = State.Stopped;
|
||||
public State CurrentState { get { return _state; } }
|
||||
|
||||
public void TakeAction(Action action)
|
||||
{
|
||||
State state = _state;
|
||||
switch (_state, action)
|
||||
{
|
||||
case (State.Stopped, Action.Start):
|
||||
state = State.Started;
|
||||
break;
|
||||
case (State.Started, Action.Accelerate):
|
||||
state = State.Running;
|
||||
break;
|
||||
case (State.Started, Action.Stop):
|
||||
state = State.Stopped;
|
||||
break;
|
||||
case (State.Running, Action.Stop):
|
||||
state = State.Stopped;
|
||||
break;
|
||||
}
|
||||
|
||||
_state = state;
|
||||
|
||||
// _state = (_state, action) switch
|
||||
// {
|
||||
// (State.Stopped, Action.Start) => State.Started,
|
||||
// (State.Started, Action.Accelerate) => State.Running,
|
||||
// (State.Started, Action.Stop) => State.Stopped,
|
||||
// (State.Running, Action.Stop) => State.Stopped,
|
||||
// _ => _state
|
||||
// };
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user