Command
Context: Encapsulate a request as an object, thereby letting you parameterize clients with queues, requests, and operations.
public interface ICommand{ void Execute();}
public class Receiver{ public void Action() => Console.WriteLine("Receiver action");}
public class ConcreteCommand : ICommand{ private Receiver _receiver; public ConcreteCommand(Receiver receiver) => _receiver = receiver; public void Execute() => _receiver.Action();}
public class Invoker{ private ICommand _command; public void SetCommand(ICommand command) => _command = command; public void PressButton() => _command.Execute();}Real-world usage example
Section titled “Real-world usage example”Undo/redo in editors: Each user action (insert, delete, format) becomes a command with Execute() and Undo(). The history stack stores commands for undo/redo.
Example: In ASP.NET Core MVC, controller actions are command objects. Also, ICommand in WPF/MVVM and BackgroundService with command queuing.