The command behavioral pattern allows you to represent an action that you want to perform as an object that can be copied from place to place and performed at a later time if you want.
You start by creating an abstract class called Command with a method called execute. It will help if you give the execute method a parameter so that it’ll know what the target of the command should be. You can also declare a method called undo which will allow you to implement a full-featured undo/redo system in your application.
Then you declare concrete classes that derive from the Command class that implement the execute method. The episode explains how to implement a command to cause a character in a game to move forward.
Now that you have this, you can refer to the command class anywhere in your code that you need to perform some action. This could be a user-initiated action such as pressing a key or maybe an automated action that’s performed on a schedule. You can also pass command objects around in your code. Maybe you have some code that detects what keys are being pressed. This code could first use the command pattern to map commands to different keys. Once it finds the correct Command object, it can pass that Command to other code that’s aware of the target. This decouples the command from the mechanism or button used to trigger the command and from the target.
Now code can use the moveForwardCommand class object to move any character in the game forward.