C Command Mode Usage Example

  • 2021-07-01 08:05:57
  • OfStack

This article illustrates the C # command pattern. Share it for your reference. The specific implementation method is as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace  Command mode 
{
  class Program
  {
    static void Main(string[] args)
    {
      Receiver r = new Receiver();
      Command c = new ConcreteCommand(r);
      Invoker i = new Invoker();
      i.SetCommand(c);
      i.ExectueCommand();
    }
    public abstract class Command
    {
      private Receiver receiver;
      internal Receiver Receiver
      {
        get { return receiver; }
        set { receiver = value; }
      }
      public Command(Receiver receiver)
      {
        this.receiver = receiver;
      }
      public abstract void Execute();
    }
    public class Receiver
    {
      public void Action()
      {
        Console.WriteLine(" Acquire receiver Adj. action Method! ");
      }
    }
    public class ConcreteCommand : Command
    {
      public ConcreteCommand(Receiver receiver) : base(receiver) { }
      public override void Execute()
      {
        Receiver.Action();
      }
    }
    public class Invoker
    {
      private Command command;
 
      internal Command Command
      {
        get { return command; }
        set { command = value; }
      }
      public void SetCommand(Command command)
      {
        this.command = command;
      }
      public void ExectueCommand()
      {
        command.Execute();
      }
    }
  }
}

I hope this article is helpful to everyone's C # programming.


Related articles: