Design Patterns: Factory

The factory design pattern consists in a class responsible for creating instances of the requested types. It solves the problem of having decisions all over the place to decide what type it should create.

For example, if we have an application that needs different logic depending on a configuration we can isolate the place where the decision is made:

  1. using System;  
  2. using System.Collections.Generic;  
  3.    
  4. namespace ConsoleApplication1  
  5. {  
  6.     public static class AppConfig  
  7.     {  
  8.         public static string SiteName = "MangaFox";  
  9.                                    // or MangaAccess  
  10.     }  
  11.    
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             var factory = new MangaReaderFactory();  
  17.    
  18.             // request an instance for the current config  
  19.             MangaReader reader = factory.Create();  
  20.    
  21.             // no decision taking, just plain logic  
  22.             reader.GetChapters();  
  23.             reader.GetPages(0);  
  24.    
  25.             Console.ReadKey(true);  
  26.         }  
  27.     }  
  28.    
  29.     public class MangaReaderFactory  
  30.     {  
  31.         public MangaReader Create()  
  32.         {  
  33.             // an instance will be chosen accordingly to the config  
  34.             switch (AppConfig.SiteName)  
  35.             {  
  36.                 case "MangaFox":  
  37.                     return new MangaFox();  
  38.                 case "MangaAccess":  
  39.                     return new MangaAccess();  
  40.             }  
  41.    
  42.             throw new ArgumentException("Invalid site name"  
  43.                 , "SiteName");  
  44.         }  
  45.     }  
  46.    
  47.     public abstract class MangaReader  
  48.     {  
  49.         public abstract IEnumerable<string> GetChapters();  
  50.         public abstract IEnumerable<string> GetPages(int ch);  
  51.     }  
  52.    
  53.     // specific logic for MangaFox  
  54.     public class MangaFox : MangaReader  
  55.     {  
  56.         public override IEnumerable<string> GetChapters()  
  57.         {  
  58.             throw new NotImplementedException();  
  59.         }  
  60.    
  61.         public override IEnumerable<string> GetPages(int ch)  
  62.         {  
  63.             throw new NotImplementedException();  
  64.         }  
  65.     }  
  66.    
  67.     // specific logic for MangaAccess  
  68.     public class MangaAccess : MangaReader  
  69.     {  
  70.         public override IEnumerable<string> GetChapters()  
  71.         {  
  72.             throw new NotImplementedException();  
  73.         }  
  74.    
  75.         public override IEnumerable<string> GetPages(int ch)  
  76.         {  
  77.             throw new NotImplementedException();  
  78.         }  
  79.     }  
  80.    
  81. }  
Without the Factory you would have to take decisions directly on your code, or decide which instance to create without a central place to take care of that for you. 
Next Recommended Reading Factory Design Pattern In C#