How to use the command design pattern in C#


Design patterns are proven solutions used to solve common design problems and reduce the complexities in code. The Gang of Four design patterns fall into three categories:

  • Creational — patterns related to object creation
  • Structural — patterns related to object assembly
  • Behavioral — patterns related to object collaboration and separation of responsibilities

The command design pattern falls under the behavioral pattern category. This article explores how we can work with the command design pattern in C#.

What is the command design pattern?

The intent of the command design pattern is to decouple the requester of an action from the object that executes the action. In the command design pattern, a request is encapsulated as an object that contains all information about the request. This object is then passed to an invoker object. The invoker object then looks for the appropriate object to handle the command and passes the command to the object.