Emiliatan

Strategy

Feb 3rd, 2019
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Strategy
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Context context;
  14.  
  15.             context = new Context(new ConcreteStrategyA());
  16.             context.ContextInterface();
  17.  
  18.             context = new Context(new ConcreteStrategyB());
  19.             context.ContextInterface();
  20.  
  21.             context = new Context(new ConcreteStrategyC());
  22.             context.ContextInterface();
  23.  
  24.             Console.Read();
  25.         }
  26.     }
  27.  
  28.  
  29.  
  30.     abstract class Strategy
  31.     {
  32.         public abstract void AlgorithmInterface();
  33.     }
  34.  
  35.     class ConcreteStrategyA : Strategy
  36.     {
  37.         public override void AlgorithmInterface()
  38.         {
  39.             Console.WriteLine("Algorithm A");
  40.         }
  41.     }
  42.  
  43.     class ConcreteStrategyB : Strategy
  44.     {
  45.         public override void AlgorithmInterface()
  46.         {
  47.             Console.WriteLine("Algorithm B");
  48.         }
  49.     }
  50.  
  51.     class ConcreteStrategyC : Strategy
  52.     {
  53.         public override void AlgorithmInterface()
  54.         {
  55.             Console.WriteLine("Algorithm C");
  56.         }
  57.     }
  58.     class Context
  59.     {
  60.         Strategy strategy;
  61.         public Context(Strategy strategy)
  62.         {
  63.             this.strategy = strategy;
  64.         }
  65.         public void ContextInterface()
  66.         {
  67.             strategy.AlgorithmInterface();
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment