I’m considering this design pattern for a simple iOS tic-tac-toe game:
GameStatesingleton class that keeps track of X and O locations on the boardGameDisplaysingleton class that handles the display of the board and the touch events
This separation of state and display seems reasonable imo. GameDisplay can forward taps on the board to GameState where the board is updated and winning conditions are checked. GameState can in turn tell GameDisplay to draw another X/O or that the game is complete and who the winner is. My current plan is to use methods like GameState.playAtSquare(Square s) to communicate between the two singletons.
Does this design fall victim to any major downsides of using singletons? iirc there’s some controversy over using singleton classes.
I argue that you should not use a Singleton in this case.
Use an object to represent
GameState(orGame), as it enables you to allow for (for instance) multiple games at once.The
GameDisplayshould be a single instance object, which is different than a Singleton. TheGameDisplayobject should be passed to methods which needs it through Dependency Injection.