namespace RefactoringGuru.DesignPatterns.Command.Conceptual
public interface ICommand
class SimpleCommand : ICommand
private string _payload = string.Empty;
public SimpleCommand(string payload)
Console.WriteLine($"SimpleCommand: See, I can do simple things like printing ({this._payload})");
class ComplexCommand : ICommand
private Receiver _receiver;
public ComplexCommand(Receiver receiver, string a, string b)
this._receiver = receiver;
Console.WriteLine("ComplexCommand: Complex stuff should be done by a receiver object.");
this._receiver.DoSomething(this._a);
this._receiver.DoSomethingElse(this._b);
public void DoSomething(string a)
Console.WriteLine($"Receiver: Working on ({a}.)");
public void DoSomethingElse(string b)
Console.WriteLine($"Receiver: Also working on ({b}.)");
private ICommand _onStart;
private ICommand _onFinish;
public void SetOnStart(ICommand command)
public void SetOnFinish(ICommand command)
this._onFinish = command;
public void DoSomethingImportant()
Console.WriteLine("Invoker: Does anybody want something done before I begin?");
if (this._onStart is ICommand)
Console.WriteLine("Invoker: ...doing something really important...");
Console.WriteLine("Invoker: Does anybody want something done after I finish?");
if (this._onFinish is ICommand)
this._onFinish.Execute();
static void Main(string[] args)
Invoker invoker = new Invoker();
invoker.SetOnStart(new SimpleCommand("Say Hi!"));
Receiver receiver = new Receiver();
invoker.SetOnFinish(new ComplexCommand(receiver, "Send email", "Save report"));
invoker.DoSomethingImportant();