mercredi 31 décembre 2014

Is there a good design pattern to "wrap" logic classes together to handle the flow of a game?

The topic may sound vague. That is because I couldn't find good words to summarize my question. I'm developing a tetris game as a school programming project (Java), and one of our tasks is to try to follow clean-code principles as closely as possible.


I've programmed Java for a while now, and I've only just begun to come across "design patterns". I found myself already following some (for example, the observer pattern), and got kind of hooked. For this particular problem I'm not looking necessarily for some out-of-the-book design patterns, but advice in general on how I could maybe improve my current design. I also want to know how my current design differs from the mediator pattern




In my current design, I have the class Game, which provides some fundamental logic for moving tetrominoes (tetris pieces) on the grid, removing filled rows, determining whether or not the game is over, etc. This class holds information of the currently falling tetromino, and the next tetromino that will start falling after the currently falling one has fallen.


I also have the class ScoreHandler, which defines how the scoring of each action should be determined. This class only holds information of the current score, and a way of updating the score.


I also have the class CommandHandler that keeps a registry of user commands. For example, the registry could hold an entry (keyCode, Command), where keyCode indicates some keyboard input, which points to a command that does stuff.


Finally, I have the class GameHandler that knows an instance of all the classes above. This class takes care of the game loop; it tells Game to play a round every X milliseconds and then notifies ScoreHandler of any cleared rows. Anywhere in-between the CommandHandler may notify the game handler that a user command was invoked, and the handler sends the command to the game instance which again updates its state according to the command.


In short the GameHandler class takes care of the flow of the game; it plays the game in a loop until some condition is met, updates the ScoreHandler's score and redirects any user commands to the game.




The Question(s)


Does my current design follow any known pattern? How does it differ from the mediator pattern? Is there an obviously better way of handling this kind of logic?


Aucun commentaire:

Enregistrer un commentaire