
In a game, or in any other program, it is customary to store game session data in a common place so that different views and controllers access them. The data might be game state, player score or user preferences. One place to store them is as global variables. This is acceptable for a small program, but as your program grows larger, it is easy to loose track of them.
I've been using the singleton pattern to store common variables. For those that are new to singletons, it is simply an object that exist as a single instance. It is instantiated at the start of the application (or when first accessed) and is retained in memory until the end.
I'm naming these singleton classes Managers in my program. For example, there is a GameManager class for holding the game state, a ViewManager that knows about all the views, and a SoundManager that is used to play game sounds.
I'm using the singleton template written by Matt Gallagher for simplicity, but you can certainly write your own. You can find more information about singletons on his blog post titled Singletons, AppDelegates and top-level data.
No comments:
Post a Comment