Giving partial access to a class using an interface
The solution is an interface. While it is possible to pass a reference of GameEngine
from GameEngine
to GameState
, this isn't desirable. What we need is a way to give GameState
direct but limited control. If it had a full reference to GameEngine
, it is likely that as the project progressed, it would end up creating problems because GameState
has too much access to GameEngine
. For example, what if GameState
decided to pause the game at the wrong time?
Interface refresher
If you think back to Chapter 8, Object-Oriented Programming, an interface is a class without any method bodies. A class can implement an interface and when it does, it must provide the body (including the code) for that method or methods. Furthermore, when a class implements an interface, it is an object of that type. When a class is a specific type, it can be used polymorphically as that type even if it is other types as well. Here are some examples of...