Caminhoneiro

Strategy

Oct 18th, 2018
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. //1 Identify an algorithm that the client would prefer to access through a "flex point."
  5.  
  6. //2 Declare the common interface for all variations of the algorithm.
  7.  
  8. //3 One-by-one, extract all algorithms into their own classes.They all should follow the common Strategy interface.
  9.  
  10. //4  Add a field for storing a reference to the current strategy, as well as a setter to change it, into the Context class.
  11. //The Context should work with this object only using the Strategy interface.
  12.  
  13. //5 Context's clients must provide it with a proper strategy objects when they need the Context to perform the work in a certain way.
  14.  
  15.  
  16.  
  17. namespace Strategy
  18. {
  19.  
  20.     class Context
  21.     {
  22.         private IStrategy _strategy;
  23.  
  24.         public Context()
  25.         {
  26.  
  27.         }
  28.  
  29.         public Context(IStrategy strategy)
  30.         {
  31.             this._strategy = strategy;
  32.         }
  33.  
  34.         public void setStrategy(IStrategy strategy)
  35.         {
  36.             this._strategy = strategy;
  37.         }
  38.  
  39.         public void DoSomeBusinessLogic()
  40.         {
  41.             Console.Write("Context: Sorting data using the strategy (not sure how it'll do it)\n");
  42.             var result = this._strategy.DoAlgorithm(new List<string> { "a", "b", "c", "d", "e" });
  43.  
  44.             string result_str = string.Empty;
  45.             foreach (var element in result as List<string>)
  46.             {
  47.                 result_str += element + ",";
  48.             }
  49.  
  50.             Console.Write(result_str);
  51.         }
  52.        
  53.     }
  54.  
  55.     interface IStrategy
  56.     {
  57.         object DoAlgorithm(object data);
  58.     }
  59.  
  60.     class ConcreteStrategyA : IStrategy
  61.     {
  62.         public object DoAlgorithm(object data)
  63.         {
  64.             var list = data as List<string>;
  65.             list.Sort();
  66.  
  67.             return list;
  68.         }
  69.     }
  70.  
  71.     class ConcreteStrategyB : IStrategy
  72.     {
  73.         public object DoAlgorithm(object data)
  74.         {
  75.             var list = data as List<string>;
  76.             list.Sort();
  77.             list.Reverse();
  78.  
  79.             return list;
  80.         }
  81.     }
  82.  
  83.    
  84.  
  85.     class Client
  86.     {
  87.         public static void ClientCode()
  88.         {
  89.             var context = new Context();
  90.  
  91.             Console.Write("Client: Strategy is set to normal sorting.\n");
  92.             context.setStrategy(new ConcreteStrategyA());
  93.             context.DoSomeBusinessLogic();
  94.             Console.Write("\n");
  95.             Console.Write("Client: Strategy is set to reverse sorting.\n");
  96.             context.setStrategy(new ConcreteStrategyB());
  97.             context.DoSomeBusinessLogic();
  98.  
  99.             Console.Write("\n");
  100.         }
  101.     }
  102.  
  103.  
  104.     class Program
  105.     {
  106.         static void Main(string[] args)
  107.         {
  108.             Console.WriteLine("Start Strategy!");
  109.  
  110.             Client.ClientCode();
  111.  
  112.             Console.ReadKey();
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment