Modeling a State
Let’s look at how to model a state by using C++ code. Listing 2 illustrates a basic state class.
Listing 2 Our most basic state class.
class State { public: virtual char* whatCanWeDo(void); protected: explicit State(); void ChangeState(ECommerceEngine* enginePtr, State* statePtr); };
The State class is the base class used by all derived state classes. This class provides one public member function called whatCanWeDo() that allows us to determine the capabilities of the state. We’ll see the use of this later; for now, take a look at the first printf() statement in Listing 1. I use a protected constructor for the use of derived subclasses. Finally, another protected member function allows the state to be changed.