Advertisement
khang_le

Refactor Switch Statements

Jul 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CodeKata
  4. {
  5.     public class Kata
  6.     {
  7.         private readonly Status _status;
  8.  
  9.         public Kata(Status status)
  10.         {
  11.             _status = status;
  12.         }
  13.  
  14.         public Kata()
  15.         {
  16.         }
  17.  
  18.         public string GetStatusDescription()
  19.         {
  20.             if(_status == null)
  21.             {
  22.                 return "I have never been set";
  23.             }
  24.             else {
  25.                 return _status.GetStatusDescription();
  26.             }
  27.         }
  28.     }
  29.         public abstract class Status
  30.         {
  31.             public abstract string GetStatusDescription();
  32.         }
  33.  
  34.         public class NewStatus : Status
  35.         {
  36.             public override string GetStatusDescription()
  37.             {
  38.                 return "I am new!";
  39.             }
  40.         }
  41.         public class ActiveStatus : Status
  42.         {
  43.             public override string GetStatusDescription()
  44.             {
  45.                 return "I am active";
  46.             }
  47.         }
  48.         public class DeactivatedStatus : Status
  49.         {
  50.             public override string GetStatusDescription()
  51.             {
  52.                 return "I have been deactivated";
  53.             }
  54.         }
  55.     public class ExampleTestCases
  56.     {
  57.               static void Main(string[] args) {
  58.             string status;
  59.             status = new Kata().GetStatusDescription();
  60.             Console.WriteLine(status);
  61.  
  62.             status = new Kata(new NewStatus()).GetStatusDescription();
  63.             Console.WriteLine(status);
  64.  
  65.             status = new Kata(new ActiveStatus()).GetStatusDescription();
  66.             Console.WriteLine(status);
  67.  
  68.             status = new Kata(new DeactivatedStatus()).GetStatusDescription();
  69.             Console.WriteLine(status);
  70.  
  71.             Console.ReadKey();
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement