Factory Without Switch Case

A factory method allows for an easy way to create objects when multiple classes are involved. It eases the way of object creation but allows the subclass to decide which object needs to be created.

It would be tedious when the client needs to specify the class name while creating the objects. So, to resolve this problem, we can use Factory pattern.

A Simple factory pattern Sample 

  1. using System;  
  2.   
  3. class Program  
  4. {  
  5.     abstract class Animal  
  6.     {  
  7.         public abstract string Title { get; }  
  8.     }  
  9.   
  10.     class Dog : Animal  
  11.     {  
  12.         public override string Title  
  13.         {  
  14.             get  
  15.             {  
  16.                 return "Dog";  
  17.             }  
  18.         }  
  19.     }  
  20.   
  21.     class Cat : Animal  
  22.     {  
  23.         public override string Title  
  24.         {  
  25.             get  
  26.             {  
  27.                 return "Cat";  
  28.             }  
  29.         }  
  30.     }  
  31.   
  32.     class Fish : Animal  
  33.     {  
  34.         public override string Title  
  35.         {  
  36.             get  
  37.             {  
  38.                 return "Fish";  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43.     static class Factory  
  44.     {  
  45.         public static Animal Get(int id)  
  46.         {  
  47.             switch (id)  
  48.             {  
  49.                 case 0:  
  50.                     return new Dog();  
  51.                 case 1:  
  52.                 case 2:  
  53.                     return new Cat();  
  54.                 case 3:  
  55.                 default:  
  56.                     return new Fish();  
  57.             }  
  58.         }  
  59.   
  60.   
  61.         static void Main()  
  62.         {  
  63.             for (int i = 0; i <= 3; i++)  
  64.             {  
  65.                 var position = Factory.Get(i);  
  66.                 Console.WriteLine("Where id = {0}, position = {1} ", i, position.Title);  
  67.   
  68.             }  
  69.             Console.ReadLine();  
  70.         }  
  71.     }  
  72. }  
Now, in case we have many objects, then we need to write multiple Switch cases. We can avoid multiple Switch case by using a dictionary returning a delegate.
 
Factory pattern without Switch case 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. class Program  
  5. {  
  6.     public abstract class Animal  
  7.     {  
  8.         public abstract string Title { get; }  
  9.     }  
  10.   
  11.     public  class Dog : Animal  
  12.     {  
  13.         public override string Title  
  14.         {  
  15.             get  
  16.             {  
  17.                 return "Dog";  
  18.             }  
  19.         }  
  20.     }  
  21.   
  22.     public  class Cat : Animal  
  23.     {  
  24.         public override string Title  
  25.         {  
  26.             get  
  27.             {  
  28.                 return "Cat";  
  29.             }  
  30.         }  
  31.     }  
  32.   
  33.     public  class Fish : Animal  
  34.     {  
  35.         public override string Title  
  36.         {  
  37.             get  
  38.             {  
  39.                 return "Fish";  
  40.             }  
  41.         }  
  42.     }  
  43.   
  44.     public static class Factory  
  45.     {  
  46.   
  47.         public static  Animal Get(int id)  
  48.         {  
  49.             var factory = cardFactories[id];  
  50.             return factory();  
  51.         }  
  52.      
  53.   
  54.       public static  Dictionary<int, Func<Animal>> cardFactories =  
  55.                     new Dictionary<int, Func<Animal>>  
  56.                 {  
  57.                     { 0, ()=>new Dog() },  
  58.                     { 1, ()=>new Cat() },  
  59.                     { 2, ()=>new Fish() },  
  60.   
  61.                 };  
  62.           
  63.     }  
  64.   
  65.     static void Main()  
  66.     {  
  67.         for (int i = 0; i <= 3; i++)  
  68.         {  
  69.             var position = Factory.Get(i);  
  70.             Console.WriteLine("Where id = {0}, position = {1} ", i, position.Title);  
  71.               
  72.         }  
  73.         Console.ReadLine();  
  74.     }  
  75. }  
It is a neat way that needs fewer lines to code.
Next Recommended Reading enum with switch case