Command Pattern Introduction


What is the command design pattern? Here we are in the context of object-oriented programming (OOP). The command pattern is a behavioral design pattern where an object is used to encapsulate all information needed to perform an action or trigger an event at a later time.

In the book Head First Design Patterns., the example of the design pattern they use is in-home automation. Christopher Okhravi has done a series of YouTube videos on each of the patterns. The command pattern is about encapsulating a command that a sender is sending to the receiver. We are not encapsulating the sender or the receiver.

The example used in the book Head First Design Patterns is home automation. Think of a small remote control that has four buttons: on, off, up, and down. These buttons could be programmable. The remote control is an invoker. The invoker is loaded with commands. In UML terms, these commands are interfaces called ICommand. A concrete command implements the command interface ICommand. The concrete command itself acts upon a receiver. The receiver might be a light bulb in your home.