We are going to review our code example in several steps:
- Let's start by writing down the main interface that each of our concrete state classes will implement:
namespace Chapter.State
{
public interface IBikeState
{
void Handle(BikeController controller);
}
}
We should note that we are passing an instance of BikeController in the Handle() method. This approach permits state classes to access public properties of BikeController. This approach might deviate slightly from tradition as usually, it's the Context object that gets passed to the states.
However, nothing stops us from passing both the Context object and the instance of BikeController to the state classes. Alternatively, we could set an instance reference to BikeController when we initialize each state class.
However, it was just simpler to do, as we are going to do for this use case.
- Now that we have our interface, let's implement the context class:
namespace...