The Command Pattern in Sport Games

The command pattern is one of my favorite design patterns. For this, I wanted to blog about for some time. After a short general introduction, we will use a handball sport game as an applied example, such that we can highlight potentials of the pattern that are not so well known.

You can find an overview of some patterns in a lecture of mine (to be published at Youtube) that I have given in April 2022 for Bio- and Chemical Engineers at TU Dortmund University. Beside that I recommend the books [1] "Design Patterns: Elements of Reusable Object-Oriented Software." by the gang of four, as [2] „Refactoring: Improving the Design of Existing Code." of Martin Fowler.


We will use object to encapsulate actions. By this, we can fulfill several requirements that are often found in software development:

  1. Same action but different triggers. (e.g. User inputs, network packets, reoccurring actions like an autosave.)
  2. A sequence of simple actions can be used to create a more complex action.
  3. An action may be executed at a later point in time
  4. It may be needed to rollback an action.

Another important aspect is the single responsibility principle (SRP). With the help of the Command pattern we separate the data of an action from its functionality. By this, the creation of actions is unified at different places though out our code base.

Classic Example: Command Pattern as UML Diagram

Classic example for a Command pattern by [3]

The classical applied example of the Command pattern is an editor. Buttons and shortcuts are senders of Commands. That means they create the commands and send them to an execute function. In the diagram, the Application object is responsible for the commands execution and therefore it is the only receiver in this case. The individual actions, Copy, Cut, Paste and Undo implement the interface Command. The stack-like container CommandHistory stores the history of actions to implement the Undo functionality.

Applied Example: Handball Game

Instead of the classical example "Editor" we want to dig into the opportunities for computer games. A undo function is not needed but we assume the following requirements for the game:

  • The simulation is in discrete time steps
  • Players or more precise their input devices and the AI can send actions
  • Replays of highlight, e.g. a goal
  • Support AI development and analyse

The following diagram uses a similar structure as the former diagram

Command Beispiel umgemünzt auf ein Handball Computerspiel

Let's dig into the requirements. First, the simulation in discrete time steps, e.g. thirty times a second, is an important assumption to guarantee reproducibility. Otherwise numerical calculations depend on the deviations of the time steps. An important assumption for the remaining requriments, but without effect on the Command pattern.

The second requirements demands that actions can be send by different senders, e.g. the input devices of players, the AI and also network packages if the game is played online.

The third requirement needs more than the Command pattern. An additional Snapshot Pattern (also Memento) is needed. This pattern can also be used for save games as it stores all relevant data at a specific point in time. In our case maybe every second such that there are different start points for replays. The CommandHistory stores the list of Commands that are played back for the replay functionality.

The fourth requirement shows the hidden strength of the Command pattern. Beside the replay of highlights, we can use it to analyze AI and player behavior. Game moves are an example, they may store bugs or are not handled well by the current AI implementation. In combination with pseudo-random number generation we are able to either repeat the same case or to investigate how well the game is balanced for a specific recorded game move. For the latter many simulations of the game move are required, such that more complex concepts as the separation of rendering and game logic are needed to also fulfill this requirement.


We digged into the popular design pattern Command. Beside the classical applied example, an editor, we applied the pattern on a Handball sport game. Many Use-Cases can be fulfilled with the Command pattern. The Command pattern is a good foundation for a replay feature but to rollback to past simulation time the Snapshot pattern is also worth a look. On top, Use-cases like AI analyses against pre-recorded game moves of players are implementable by using the Command pattern.


[1] Gamma, E., Helm, R., Johnson, R., Vlissides, J. M. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional. ISBN: 0201633612
[2] Fowler, M. (1999). Refactoring: Improving the Design of Existing Code. Boston, MA, USA: Addison-Wesley. ISBN: 0-201-48567-2

Leave a Reply