This is a very simple pattern. It simply is an object that does a command. It is defined by an Interface with a run() method like this:
An interface doesn’t get much simpler than this.
So how might we use it to good effect?
If we are creating other Classes that are going to do things, we can implement this interface and create another class to manage and initialize the commands. Here is an example of a class that implements Command:
This next class would store and run the commands:
And here is an example. You could batch emails to send, or group them as you wished. You could use code to schedule them to be sent at intervals like every 90 seconds. There are many possibilities.
Other uses for the pattern might include maintaining a list of commands that have been executed in a user interface and implementing an undo / redo mechanism for the actions.