Factory Method Without Switch Case

A factory method affords us 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. usingSystem;  
  2. classProgram {  
  3.     abstract class Animal {  
  4.         public abstract string Title {  
  5.             get;  
  6.         }  
  7.     }  
  8.     classDog: Animal {  
  9.         publicoverride string Title {  
  10.             get {  
  11.                 return "Dog";  
  12.             }  
  13.         }  
  14.     }  
  15.     classCat: Animal {  
  16.         publicoverride string Title {  
  17.             get {  
  18.                 return "Cat";  
  19.             }  
  20.         }  
  21.     }  
  22.     classFish: Animal {  
  23.         publicoverride string Title {  
  24.             get {  
  25.                 return "Fish";  
  26.             }  
  27.         }  
  28.     }  
  29.     staticclass Factory {  
  30.         publicstatic Animal Get(int id) {  
  31.             switch (id) {  
  32.                 case0: returnnew Dog();  
  33.                 case1: case2: returnnew Cat();  
  34.                 case3: default: returnnew Fish();  
  35.             }  
  36.         }  
  37.         staticvoid Main() {  
  38.             for (int i = 0; i <= 3; i++) {  
  39.                 var position = Factory.Get(i);  
  40.                 WriteLine("Where id = {0}, position = {1} ", i, position.Title);  
  41.             }  
  42.             ReadLine();  
  43.         }  
  44.     }  
  45. }  
Now, in case we have many objects, we need to write multiple Switch cases. We can avoid multiple Switch cases by using a dictionary returning a delegate. 

Factory pattern without Switch case

  1. usingSystem;  
  2. usingCollections.Generic;  
  3. classProgram {  
  4.     public abstract class Animal {  
  5.         public abstract string Title {  
  6.             get;  
  7.         }  
  8.     }  
  9.     publicclass Dog: Animal {  
  10.         publicoverride string Title {  
  11.             get {  
  12.                 return "Dog";  
  13.             }  
  14.         }  
  15.     }  
  16.     publicclass Cat: Animal {  
  17.         publicoverride string Title {  
  18.             get {  
  19.                 return "Cat";  
  20.             }  
  21.         }  
  22.     }  
  23.     publicclass Fish: Animal {  
  24.         publicoverride string Title {  
  25.             get {  
  26.                 return "Fish";  
  27.             }  
  28.         }  
  29.     }  
  30.     publicstatic class Factory {  
  31.         publicstatic Animal Get(int id) {  
  32.             var factory = cardFactories[id];  
  33.             returnfactory();  
  34.         }  
  35.         publicstatic Dictionary < int, Func < Animal >> cardFactories = newDictionary < int, Func < Animal >> {  
  36.             {  
  37.                 0,  
  38.                 () => newDog()  
  39.             },  
  40.             {  
  41.                 1,  
  42.                 () => newCat()  
  43.             },  
  44.             {  
  45.                 2,  
  46.                 () => newFish()  
  47.             },  
  48.         };  
  49.     }  
  50.     staticvoid Main() {  
  51.         for (int i = 0; i <= 3; i++) {  
  52.             var position = Factory.Get(i);  
  53.             WriteLine("Where id = {0}, position = {1} ", i, position.Title);  
  54.         }  
  55.         ReadLine();  
  56.     }  
  57. }  

It is a neat way that needs fewer lines to code.