Setting up the StateManager controller
Now, look at line 6 of the StateManager
class in the following screenshot. We have a big problem right off the bat:

The
activeState
variable needs to be able to store all of the State types. Right now it can only store a reference to a BeginState
type of object. This looks like a huge problem! What about the classes PlayState
, WonState
, and LostState
? What if we had 50 different States that needed to be referenced in activeState
?
The following diagram is our dilemma:

Studying an example of inheritance
Let's look at this issue using objects we all use all the time.
How about a Potato? Let's also imagine we have a Potato bag. Now to connect these real objects into the scripting world, the following is a simple declared variable:
public Potato bag;
So we have a variable named bag
. The type of object it can store is a Potato
. Saying this in another way: I have a Potato bag, and the only thing I can put in it is a potato.
The issue is shown in the following diagram...