Advertisement
Slash18

Command

Jun 27th, 2016
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1.  
  2. //
  3. //Full Tutorial on https://indiedevart.wordpress.com/
  4. //
  5.  
  6. using System;
  7.  
  8. namespace Command.Pattern
  9. {
  10.     class Program
  11.     {
  12.         static void Main()
  13.         {
  14.             // Create receiver, command, and invoker
  15.             IEnemy receiver = new Goblin();
  16.             ICommand command = new Heal(receiver);
  17.             Field invoker = new Field();
  18.  
  19.             // Set and execute command
  20.             invoker.SetCommand(command);
  21.             invoker.EnterField();
  22.             invoker.ExitField();
  23.            
  24.             Console.ReadKey();
  25.         }
  26.     }
  27.  
  28.     interface ICommand
  29.     {
  30.  
  31.         void Execute();
  32.         void UnDo();
  33.  
  34.     }
  35.  
  36.     class Heal : ICommand
  37.     {
  38.          IEnemy _receiver;
  39.         // Constructor
  40.         public Heal(IEnemy receiver)
  41.         {
  42.             _receiver = receiver;
  43.         }
  44.  
  45.         public void Execute()
  46.         {
  47.             Console.WriteLine(_receiver.Heal());
  48.         }
  49.         public void UnDo()
  50.         {
  51.             Console.WriteLine(_receiver.Damage());
  52.         }
  53.     }
  54.     class Buff : ICommand
  55.     {
  56.         IEnemy _receiver;
  57.         // Constructor
  58.         public Buff(IEnemy receiver)
  59.         {
  60.             _receiver = receiver;
  61.         }
  62.  
  63.         public void Execute()
  64.         {
  65.             Console.WriteLine(_receiver.BuffON());
  66.         }
  67.         public void UnDo()
  68.         {
  69.             Console.WriteLine(_receiver.BuffOFF());
  70.         }
  71.     }
  72.  
  73.     interface IEnemy
  74.     {
  75.         string Damage();
  76.         string Heal();
  77.         string BuffON();
  78.         string BuffOFF();
  79.     }
  80.     class Goblin : IEnemy
  81.     {
  82.         public string BuffOFF()
  83.         {
  84.             return "Goblin enters the field-Buff is ON";
  85.         }
  86.  
  87.         public string BuffON()
  88.         {
  89.             return "Goblin exits the field-Buff is OFF";
  90.         }
  91.  
  92.         public string Damage()
  93.         {
  94.             return "Goblin exits the field- takes damage";
  95.         }
  96.  
  97.         public string Heal()
  98.         {
  99.             return "Goblin enters the field- heals";
  100.         }
  101.     }
  102.     class Dragon : IEnemy
  103.     {
  104.         public string BuffOFF()
  105.         {
  106.             return "Goblin enters the field-Buff is ON";
  107.         }
  108.  
  109.         public string BuffON()
  110.         {
  111.             return "Goblin exits the field-Buff is OFF";
  112.         }
  113.  
  114.         public string Damage()
  115.         {
  116.             return "Goblin exits the field- takes damage";
  117.         }
  118.  
  119.         public string Heal()
  120.         {
  121.             return "Goblin enters the field- heals";
  122.         }
  123.     }
  124.     class Field
  125.     {
  126.         private ICommand _command;
  127.  
  128.         public void SetCommand(ICommand command)
  129.         {
  130.             this._command = command;
  131.         }
  132.  
  133.         public void EnterField()
  134.         {
  135.             _command.Execute();
  136.         }
  137.         public void ExitField()
  138.         {
  139.             _command.UnDo();
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement