Why To Use Factory Method in C#

Look at the following points:

  1. Okay. We will start with a simple party example.

  2. Consider the following scenario.
    1.     public void partynighty(string brand)  
    2.     {  
    3.         if (brand == "MH")  
    4.         {  
    5.             Alcohol ol = new MH();  
    6.         }  
    7.         if (brand == "MC")  
    8.         {  
    9.             Alcohol ol = new MH();  
    10.         }  
    11.         if (brand == "Bacardi")  
    12.         {  
    13. Alcohol ol = new Bacardi();  
    14. }  
    15.         ol.drink();  
    16.         ol.dance();  
    17.         ol.talkthetruth();  
    18.         ol.sleepwell();  
    19.     }  
  3. I have organized a party for the night. I have invited my friends.

  4. Now, see; all my friends will like to drink their own favorite brand.

  5. But everyone, after drinking will do the same, like dancing, talking and so on.

    In this scenario the brand of alcohol will vary but the behaviour after drinking is the same across all my friends.

  6. So if I have this functionally in my main class, I should vary the brand now and then, depending upon my friend's tastes. If my class has a huge amount of functionally then it becomes harder to introduce new brands.

    Identifying what varies and what stays the same.

  7. So let's take out the initailzisation of new brands and put them into another class.

    cs code

  8. It's actually simple. See the following:
    1. public  class AlcoholFa// simple factory class which is responsible for initialing the brand class  
    2. {  
    3.     Alcohol alco;  
    4.       
    5.     public Alcohol Getmyjoy(string brand)  
    6.     {  
    7.         Alcohol ol = null;  
    8.         if (brand == "MH")  
    9.         {  
    10.             ol = new MH();  
    11.         }  
    12.         if (brand == "MC")  
    13.         {  
    14.             ol = new MH();  
    15.         }  
    16.         if (brand == "bakadi")  
    17.         {  
    18.             ol = new Bacardi();  
    19.         }  
    20.         return ol;  
    21.     }  
    22. }  
    23. public class Alcohol  
    24. {  
    25.   
    26.     private Alcohol()  
    27.     {  
    28.            
    29.     }  
    30.  public Alcohol getmydrinks(string brd)// base class which uses the “AlcoholFa” to set brand  
    31.  {  
    32.      AlcoholFa fa = new AlcoholFa();  
    33.      return fa.Getmyjoy(brd);  
    34.  }  
    35.     public void drink();  
    36.         public void dance();  
    37.         public void talkthetruth();  
    38.         public void  sleepwell();  
    39. }  
    40. public class MH : Alcohol  
    41. {  
    42.   
    43. }  
    44. public class MC : Alcohol  
    45. {  
    46.   
    47. }  
    48. public class Bacardi : Alcohol  
    49. { }  
  9. Now our main class is very simplified. It looks as in the following:
    1. Alcohol ol;  
    2. //ol = factory.Getmyjoy("MC"); '  
    3. ol = getmine("MC");  
    4. ol.drink();  
    5. ol.dance();  
    6. ol.talkthetruth();  
    7. ol.sleepwell();  
  10. That's it, we have done our job. Wait a second.

  11. Suppose we have multiple qualities of each brand, like “A Quality”, “B Quality”, “C Quality”.

  12. So what to do? Think; take your time.

  13. Here comes the Factory partner like a superhero. He said that we can create multiple factories and we even allow Clint to choose his own choice and then order his brand.

  14. So now I can add as many brands as I need dynamically. Note dynamically is important.

  15. I can even include a new set quality.

  16. Now everything in my class is dynamic so that I (the “server”; the main class) can easily order any brand in the future without modifying the mail class.

Source Code

  1. public partial class Form1 : Form  
  2. {  
  3.     public Form1()  
  4.     {  
  5.         InitializeComponent();  
  6.     }  
  7.   
  8.     private void button1_Click(object sender, EventArgs e)  
  9.     {  
  10.           
  11.   
  12.         partynighty("MC");  
  13.     }  
  14.     public void partynighty(string brand)  
  15.     {  
  16.   
  17.           AlcoholFactory ole = new AlcoholFactoryQuality_B();  
  18.           server serveme = new server(ole);  
  19.           serveme.getmine(brand);  
  20.     }  
  21.   
  22. }  
  23.   
  24. public class server  
  25. {  
  26.     AlcoholFactory factory;  
  27.     public server(AlcoholFactory fac)  
  28.     {  
  29.         this.factory = fac;  
  30.     }  
  31.     public Alcohol getmine(string brand)  
  32.     {  
  33.         Alcohol ol;  
  34.         ol = factory.Getmyjoy("MC");  
  35.         return ol;  
  36.           
  37.     }  
  38. }  
  39. public abstract class AlcoholFactory  
  40. {  
  41.     //public abstract Alcohol ol;  
  42.     public abstract Alcohol Getmyjoy(string str);  
  43. }  
  44. public class AlcoholFactoryQuality_A : AlcoholFactory  
  45. {  
  46.     //Alcohol alco;  
  47.     //public AlcoholFactoryQuality_A(Alcohol ol)  
  48.     //{  
  49.     //    this.alco = ol;  
  50.     //}  
  51.     public override Alcohol Getmyjoy(string brand)  
  52.     {  
  53.         Alcohol ol = null;  
  54.         if (brand == "MH")  
  55.         {  
  56.             ol = new MH();  
  57.         }  
  58.         if (brand == "MC")  
  59.         {  
  60.             ol = new MC();  
  61.         }  
  62.         if (brand == "bakadi")  
  63.         {  
  64.             ol = new Bacardi();  
  65.         }  
  66.         return ol;  
  67.     }  
  68. }  
  69. public class AlcoholFactoryQuality_B : AlcoholFactory  
  70. {  
  71.     //Alcohol alco;  
  72.     //public AlcoholFactoryQuality_B(Alcohol ol)  
  73.     //{  
  74.     //    this.alco = ol;  
  75.     //}  
  76.     public override Alcohol Getmyjoy(string brand)  
  77.     {  
  78.         Alcohol ol = null;  
  79.         if (brand == "MH")  
  80.         {  
  81.             ol = new MH();  
  82.         }  
  83.         if (brand == "MC")  
  84.         {  
  85.             ol = new MH();  
  86.         }  
  87.         if (brand == "bakadi")  
  88.         {  
  89.             ol = new Bacardi();  
  90.         }  
  91.         return ol;  
  92.     }  
  93. }  
  94. public class Alcohol  
  95. {  
  96.     AlcoholFactory factory;  
  97.     public Alcohol(AlcoholFactory fac)  
  98.     {  
  99.         this.factory = fac;  
  100.     }  
  101. }  
  102. public class MH : Alcohol  
  103. {  
  104.     public MH():base (null)  
  105.     { }  
  106. }  
  107. public class MC : Alcohol  
  108. {  
  109.     public MC():base (null)  
  110.     { }  
  111. }  
  112. public class Bacardi : Alcohol  
  113. {  
  114.     public Bacardi(): base(null)  
  115.     { }  
  116. }  

 

Up Next
    Ebook Download
    View all
    Learn
    View all