tri_inn

tri_inn

  • NA
  • 1.2k
  • 222.1k

C#: When one should go for factory method pattern

Apr 5 2017 10:58 AM

i understand the both factory and factory method pattern. in factory pattern we create instance of my classed by another class function dynamically where i pass some parameter to another class function and based on that parameter another class function return right instance of class.

in factory method pattern we have to proceed one further step. in factory method pattern subclass create instance of my class. i do not find a scenario where people has to go for factory method pattern. so please some one come with a scenario where normal factory pattern will not be used rather people prefer to use factory method pattern.

here i am posting two set of code first one done by factory pattern and second one done by factory design pattern

1st set of code where factory pattern used

 
  1. public enum Shipper  
  2. {  
  3.     UPS = 1,  
  4.     FedEx = 2,  
  5.     Purolator = 3  
  6. }  
  7.   
  8. public interface IShip  
  9. {  
  10.     void Ship();  
  11. }  
  12.   
  13. public class ShipperPurolator : IShip  
  14. {  
  15.     public void Ship()  
  16.     {  
  17.         //-- code logic to implement shipping method for Purolator  
  18.         MessageBox.Show("Purolator ship start");  
  19.     }  
  20. }  
  21.   
  22. public class ShipperUPS : IShip  
  23. {  
  24.     public void Ship()  
  25.     {  
  26.         //-- code logic to implement shipping method for Purolator  
  27.         MessageBox.Show("UPS ship start");  
  28.     }  
  29. }  
  30.   
  31. public class ShipperFexEx : IShip  
  32. {  
  33.     public void Ship()  
  34.     {  
  35.         //-- code logic to implement shipping method for Purolator  
  36.         MessageBox.Show("FedEx ship start");  
  37.     }  
  38. }  
  39.   
  40. public class ShipperFactory  
  41. {  
  42.     public static IShip CreateInstance(Shipper enumModuleName)  
  43.     {  
  44.         IShip objActivity = null;  
  45.   
  46.         switch (enumModuleName)  
  47.         {  
  48.             case Shipper.UPS:  
  49.                 objActivity = new ShipperUPS();  
  50.                 break;  
  51.             case Shipper.FedEx:  
  52.                 objActivity = new ShipperFexEx();  
  53.                 break;  
  54.             case Shipper.Purolator:  
  55.                 objActivity = new ShipperPurolator();  
  56.                 break;  
  57.             default:  
  58.                 break;  
  59.         }  
  60.         return objActivity;  
  61.     }  

 Calling this way
 
  1. IShip objActivity = null;  
  2.   
  3.   
  4. private void btnUPS_Click(object sender, EventArgs e)  
  5. {  
  6.     objActivity = ShipperFactory.CreateInstance(Shipper.UPS);  
  7.     objActivity.Ship();  
  8. }  
  9.   
  10. private void btnFedEx_Click(object sender, EventArgs e)  
  11. {  
  12.     objActivity = ShipperFactory.CreateInstance(Shipper.FedEx);  
  13.     objActivity.Ship();  
  14. }  
  15.   
  16. private void btnPurolator_Click(object sender, EventArgs e)  
  17. {  
  18.     objActivity = ShipperFactory.CreateInstance(Shipper.Purolator);  
  19.     objActivity.Ship();  

now same thing done by factory method pattern where i have to write more code to get the job done

  1. public interface IShip  
  2. {  
  3.     void Ship();  
  4. }  
  5.   
  6. public class ShipperPurolator : IShip  
  7. {  
  8.     public void Ship()  
  9.     {  
  10.         //-- code logic to implement shipping method for Purolator  
  11.         MessageBox.Show("Purolator ship start");  
  12.     }  
  13. }  
  14.   
  15. public class ShipperUPS : IShip  
  16. {  
  17.     public void Ship()  
  18.     {  
  19.         //-- code logic to implement shipping method for Purolator  
  20.         MessageBox.Show("UPS ship start");  
  21.     }  
  22. }  
  23.   
  24. public class ShipperFedEx : IShip  
  25. {  
  26.     public void Ship()  
  27.     {  
  28.         //-- code logic to implement shipping method for Purolator  
  29.         MessageBox.Show("FedEx ship start");  
  30.     }  
  31. }  
  32.   
  33. // factory class start  
  34.   
  35. public interface IShipFactory  
  36. {  
  37.     IShip GetShipper();    
  38. }  
  39.   
  40. public class ShipperFexExFactory : IShipFactory  
  41. {  
  42.     public IShip GetShipper()  
  43.     {  
  44.         //-- code logic to implement shipping method for Purolator  
  45.         //MessageBox.Show("FedEx ship start");  
  46.         return new ShipperFedEx();  
  47.     }  
  48. }  
  49.   
  50. public class ShipperUPSFactory : IShipFactory  
  51. {  
  52.     public IShip GetShipper()  
  53.     {  
  54.         //-- code logic to implement shipping method for Purolator  
  55.         return new ShipperUPS();  
  56.     }  
  57. }  
  58.   
  59. public class ShipperPurolatorFactory : IShipFactory  
  60. {  
  61.     public IShip GetShipper()  
  62.     {  
  63.         //-- code logic to implement shipping method for Purolator  
  64.         return new ShipperPurolator();  
  65.     }  

calling like this way

  1. IShipFactory _IShipFactory = null;  
  2.   
  3. private void btnUPS_Click(object sender, EventArgs e)  
  4. {  
  5.     _IShipFactory = new ShipperUPSFactory();  
  6.     _IShipFactory.GetShipper().Ship();  
  7. }  
  8.   
  9. private void btnFedEx_Click(object sender, EventArgs e)  
  10. {  
  11.     _IShipFactory = new ShipperFexExFactory();  
  12.     _IShipFactory.GetShipper().Ship();  
  13. }  
  14.   
  15. private void btnPurolator_Click(object sender, EventArgs e)  
  16. {  
  17.     _IShipFactory = new ShipperPurolatorFactory();  
  18.     _IShipFactory.GetShipper().Ship();  

 
please tell me a solid scenario when people feel right to choose factory method pattern. thanks

Answers (1)