fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. Context context;
  8.  
  9. // Three contexts following different strategies
  10. context = new Context(new ConcreteStrategyA());
  11. context.ContextInterface();
  12.  
  13. context = new Context(new ConcreteStrategyB());
  14. context.ContextInterface();
  15.  
  16.  
  17. // Wait for user
  18. Console.ReadKey();
  19. }
  20. }
  21.  
  22.  
  23. abstract class Strategy
  24. {
  25. public abstract void AlgorithmInterface();
  26. }
  27.  
  28. class ConcreteStrategyA : Strategy
  29. {
  30. public override void AlgorithmInterface()
  31. {
  32. Console.WriteLine(
  33. "Called ConcreteStrategyA.AlgorithmInterface()");
  34. }
  35. }
  36.  
  37. class ConcreteStrategyB : Strategy
  38. {
  39. public override void AlgorithmInterface()
  40. {
  41. Console.WriteLine(
  42. "Called ConcreteStrategyB.AlgorithmInterface()");
  43. }
  44. }
  45.  
  46. class Context
  47. {
  48. private Strategy _strategy;
  49.  
  50. // Constructor
  51. public Context(Strategy strategy)
  52. {
  53. this._strategy = strategy;
  54. }
  55.  
  56. public void ContextInterface()
  57. {
  58. _strategy.AlgorithmInterface();
  59. }
  60. }
Success #stdin #stdout 0.02s 34760KB
stdin
Standard input is empty
stdout
Called ConcreteStrategyA.AlgorithmInterface()
Called ConcreteStrategyB.AlgorithmInterface()