title: How to implement a factory that automatically finds a strategy
body: <p>I've asked myself this question and I'm still not done thinking about this.</p>
  
  <p><strong>What I am thinking about</strong></p>
  
  <p>When you have a strategy pattern, a lot of implementations also use a factory pattern to retrieve a specific strategy. Most factory examples on the internet make use of a switch or if statement. This works perfectly when you do not often change or add strategies. But what if the factory is used to dynamically find a strategy and the strategies are changed and added often. Then this is a full time job for the programmer. Now I have a situation where I just want to add a new strategy without having to change the factory. In other words how do you implement the factory pattern so it dynamically searches for a strategy. And how can I list all available strategies.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>When I look for this on the internet I can't find a good proper solution. I am thinking about using a reflection library to do this but it is not recommended to use reflection everywhere I look. So how to implement a dynamic factory. Or is there an other pattern for this purpose?</p>
  
  <p><strong>Example</strong></p>
  
  <p>The strategies:
  <a href="https://i.stack.imgur.com/izMDr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/izMDr.png" alt="enter image description here"></a></p>
  
  <p>The factory:</p>
  
  <pre><code>public enum StrategyName { ImplementedStrategy1, ImplementedStrategy2, ImplementedStrategy3 };
  
  public class StrategyFactory
  {
      public static Sniffer getInstance(StrategyName choice) {
  
          Strategy strategy = null;
  
          switch (choice) {
              case StrategyName.ImplementedStrategy1:
                  strategy = new ImplementedStrategy1();
                  break;
              case StrategyName.ImplementedStrategy2:
                  strategy = new ImplementedStrategy2();
                  break;
              case StrategyName.ImplementedStrategy3:
                  strategy = new ImplementedStrategy3();
                  break;
          }
  
          return strategy;
      }
  }
  </code></pre>
  
  <p>Now how can I make this dynamic? Or why shouldn't I?</p>