title: A Factory Pattern that will satisfy the Open/Closed Principle?
body: <p>I have the following concrete <code>Animal</code> products: <code>Dog</code> and <code>Cat</code>.</p>
  
  <p>I am using a <a href="http://www.datensarg.de/2009/11/01/parameterized-factory-desing-pattern/" rel="noreferrer">parameterized Factory method</a> to create said products. Depending on the <code>AnimalInfo</code> parameter that is passed to the Factory method, a concrete product will be created. The mapping logic is placed in the Factory method.</p>
  
  <p>Here is my code:</p>
  
  <pre><code> public abstract class AnimalInfo
      {
          public abstract String Sound { get; }
      }
  
      public class DogInfo : AnimalInfo
      {
          public override string Sound
          {
              get { return "Bark"; }
          }
      }
  
      public class CatInfo : AnimalInfo
      {
          public override string Sound
          {
              get { return "Meow"; }
          }
      }
  
      public abstract class Animal
      {
          public abstract void Talk();
      }
  
      public class Dog : Animal
      {
          private readonly DogInfo _info;
  
          public Dog(DogInfo aInfo)
          {
              _info = aInfo;
          }
  
          public override void Talk()
          {
              Console.WriteLine(_info.Sound);
          }
      }
  
      public class Cat : Animal
      {
          private readonly CatInfo _info;
  
          public Cat(CatInfo aInfo)
          {
              _info = aInfo;
          }
  
          public override void Talk()
          {
              Console.WriteLine(_info.Sound);
          }
      }
  </code></pre>
  
  <p>Here's my Factory method with its logic:</p>
  
  <pre><code>public static class AnimalFactory
  {
      public static Animal CreateAnimal(AnimalInfo aInfo)
      {
          if (aInfo is DogInfo)
              return new Dog(aInfo as DogInfo);
          if (aInfo is CatInfo)
              return new Cat(aInfo as CatInfo);
          return null;
      }
  }
  </code></pre>
  
  <p>The problem I'm seeing here is that the Factory method itself violates the <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Open/closed_principle" rel="noreferrer">Open/Closed principle</a> in such a way that if I add a new Animal, I will need to modify the Factory method to reflect the new mapping.</p>
  
  <p>Is there a way to make the creation more "dynamic" via reflection? More importantly, is there any anti-pattern in my design?</p>