Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1.  public enum Types
  2.     {
  3.         Type1,
  4.         Type2,
  5.         Type3,
  6.         Type4
  7.     }
  8.  
  9.     public interface IStrategy
  10.     {
  11.         Types Type { get; }
  12.         void Do();
  13.     }
  14.  
  15.     public class Realization1 : IStrategy
  16.     {
  17.         public Types Type => Types.Type1;
  18.         public void Do()
  19.         {
  20.             Console.WriteLine(Type.ToString());
  21.         }
  22.     }
  23.     public class Realization2 : IStrategy
  24.     {
  25.         public Types Type => Types.Type2;
  26.         public void Do()
  27.         {
  28.             Console.WriteLine("Result = " + Type.ToString());
  29.         }
  30.     }
  31.  
  32.     public class MainRealization
  33.     {
  34.         private readonly IEnumerable<IStrategy> _strategies;
  35.         public MainRealization(IEnumerable<IStrategy> strategies)
  36.         {
  37.             this._strategies = strategies;
  38.         }
  39.  
  40.         public void Do(Types requiredType)
  41.         {
  42.             var method = this._strategies.FirstOrDefault(x => x.Type == requiredType);
  43.             method.Do();
  44.         }
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement