ivandrofly

command design pattern - c#

Feb 6th, 2021 (edited)
1,445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. // github: https://github.com/ivandrofly
  2. namespace CommandPattern
  3. {
  4.     using System;
  5.  
  6.     internal interface ICommand
  7.     {
  8.         void Execute();
  9.     }
  10.  
  11.     internal interface IReceiver
  12.     {
  13.         void TurnOn();
  14.         void TurnOff();
  15.     }
  16.  
  17.     internal interface IInvoker
  18.     {
  19.         void ExecuteCommand(ICommand command);
  20.     }
  21.  
  22.     internal class UniversalController : IInvoker
  23.     {
  24.         public void ExecuteCommand(ICommand command) => command.Execute();
  25.     }
  26.  
  27.     internal class TurnOnCommand : ICommand
  28.     {
  29.         private readonly IReceiver receiver;
  30.  
  31.         public TurnOnCommand(IReceiver receiver)
  32.         {
  33.             this.receiver = receiver;
  34.         }
  35.         public void Execute()
  36.         {
  37.             Console.WriteLine($"{nameof(TurnOnCommand)} - executed!");
  38.             receiver.TurnOn();
  39.         }
  40.     }
  41.  
  42.     internal class TurnOffCommand : ICommand
  43.     {
  44.         private readonly IReceiver receiver;
  45.  
  46.         public TurnOffCommand(IReceiver receiver)
  47.         {
  48.             this.receiver = receiver;
  49.         }
  50.         public void Execute()
  51.         {
  52.             Console.WriteLine($"{nameof(TurnOffCommand)} - executed!");
  53.             receiver.TurnOff();
  54.         }
  55.     }
  56.  
  57.     internal class SamsungTv : IReceiver
  58.     {
  59.         private bool _isOn;
  60.  
  61.         public void TurnOff() => _isOn = false;
  62.         public void TurnOn() => _isOn = true;
  63.  
  64.         public void WatchTv() => Console.WriteLine(_isOn ? "Watching netflix :)" : "Be sad :(");
  65.     }
  66.  
  67.     internal class LGSmartTv : IReceiver
  68.     {
  69.         public void TurnOff()
  70.         {
  71.             Console.WriteLine("Black screen (Life is not good :()");
  72.         }
  73.  
  74.         public void TurnOn()
  75.         {
  76.             Console.WriteLine("Display LG + (Life is good)");
  77.         }
  78.     }
  79.  
  80.     internal class Program
  81.     {
  82.         private static void Main(string[] args)
  83.         {
  84.             SamsungTv samsungTv = new SamsungTv();
  85.             IReceiver lgTv = new LGSmartTv();
  86.  
  87.             // universal remove controller
  88.             IInvoker urc = new UniversalController();
  89.  
  90.             // commands
  91.             TurnOnCommand onCmd = new TurnOnCommand(samsungTv);
  92.             TurnOffCommand offCmd = new TurnOffCommand(samsungTv);
  93.  
  94.             samsungTv.WatchTv();
  95.             urc.ExecuteCommand(onCmd);
  96.             samsungTv.WatchTv();
  97.             urc.ExecuteCommand(offCmd);
  98.             samsungTv.WatchTv();
  99.  
  100.  
  101.             Console.WriteLine("press any key to execute command on lg tv");
  102.             Console.ReadLine();
  103.  
  104.             urc.ExecuteCommand(new TurnOnCommand(lgTv));
  105.             urc.ExecuteCommand(new TurnOffCommand(lgTv));
  106.  
  107.             Console.ReadLine();
  108.         }
  109.     }
  110. }
  111.  
Add Comment
Please, Sign In to add comment