tri_inn

tri_inn

  • NA
  • 1.2k
  • 223.8k

How to use factory design pattern for my scenario

Apr 4 2017 4:46 AM

now i have three class which i use to ship and those are Purolator, UPS and FedEx and each function has one common method which is Ship. now tell me how to use factory design pattern to make my code more reusable.

here is my sample code
--------------------------------
  1. public class Purolator   
  2. {  
  3.     public void Ship()  
  4.     {  
  5.         //-- code logic to implement shipping method for Purolator  
  6.     }  
  7. }  
  8.   
  9. public class UPS   
  10. {  
  11.     public void Ship()  
  12.     {  
  13.         //-- code logic to implement shipping method for UPS  
  14.     }  
  15. }  
  16.   
  17. public class FexEx   
  18. {  
  19.     public void Ship()  
  20.     {  
  21.         //-- code logic to implement shipping method for FexEx  
  22.     }  

 now i create class instance by name separately when i require to use UPS or FedEx etc but how could i use factory design pattern to make my shipping business logic more reusable. please discuss it with a example code. thanks

Answers (3)