none
C#: When one should go for factory method pattern instead of factory pattern

    Question

  • 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

    public enum Shipper
    {
        UPS = 1,
        FedEx = 2,
        Purolator = 3
    }
    
    public interface IShip
    {
        void Ship();
    }
    
    public class ShipperPurolator : IShip
    {
        public void Ship()
        {
            //-- code logic to implement shipping method for Purolator
            MessageBox.Show("Purolator ship start");
        }
    }
    
    public class ShipperUPS : IShip
    {
        public void Ship()
        {
            //-- code logic to implement shipping method for Purolator
            MessageBox.Show("UPS ship start");
        }
    }
    
    public class ShipperFexEx : IShip
    {
        public void Ship()
        {
            //-- code logic to implement shipping method for Purolator
            MessageBox.Show("FedEx ship start");
        }
    }
    
    public class ShipperFactory
    {
        public static IShip CreateInstance(Shipper enumModuleName)
        {
            IShip objActivity = null;
    
            switch (enumModuleName)
            {
                case Shipper.UPS:
                    objActivity = new ShipperUPS();
                    break;
                case Shipper.FedEx:
                    objActivity = new ShipperFexEx();
                    break;
                case Shipper.Purolator:
                    objActivity = new ShipperPurolator();
                    break;
                default:
                    break;
            }
            return objActivity;
        }
    }

    Calling this way

    IShip objActivity = null;
    
    
    private void btnUPS_Click(object sender, EventArgs e)
    {
        objActivity = ShipperFactory.CreateInstance(Shipper.UPS);
        objActivity.Ship();
    }
    
    private void btnFedEx_Click(object sender, EventArgs e)
    {
        objActivity = ShipperFactory.CreateInstance(Shipper.FedEx);
        objActivity.Ship();
    }
    
    private void btnPurolator_Click(object sender, EventArgs e)
    {
        objActivity = ShipperFactory.CreateInstance(Shipper.Purolator);
        objActivity.Ship();
    }

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

    public interface IShip
    {
        void Ship();
    }
    
    public class ShipperPurolator : IShip
    {
        public void Ship()
        {
            //-- code logic to implement shipping method for Purolator
            MessageBox.Show("Purolator ship start");
        }
    }
    
    public class ShipperUPS : IShip
    {
        public void Ship()
        {
            //-- code logic to implement shipping method for Purolator
            MessageBox.Show("UPS ship start");
        }
    }
    
    public class ShipperFedEx : IShip
    {
        public void Ship()
        {
            //-- code logic to implement shipping method for Purolator
            MessageBox.Show("FedEx ship start");
        }
    }
    
    // factory class start
    
    public interface IShipFactory
    {
        IShip GetShipper();  
    }
    
    public class ShipperFexExFactory : IShipFactory
    {
        public IShip GetShipper()
        {
            //-- code logic to implement shipping method for Purolator
            //MessageBox.Show("FedEx ship start");
            return new ShipperFedEx();
        }
    }
    
    public class ShipperUPSFactory : IShipFactory
    {
        public IShip GetShipper()
        {
            //-- code logic to implement shipping method for Purolator
            return new ShipperUPS();
        }
    }
    
    public class ShipperPurolatorFactory : IShipFactory
    {
        public IShip GetShipper()
        {
            //-- code logic to implement shipping method for Purolator
            return new ShipperPurolator();
        }
    }

    calling like this way

    IShipFactory _IShipFactory = null;
    
    private void btnUPS_Click(object sender, EventArgs e)
    {
        _IShipFactory = new ShipperUPSFactory();
        _IShipFactory.GetShipper().Ship();
    }
    
    private void btnFedEx_Click(object sender, EventArgs e)
    {
        _IShipFactory = new ShipperFexExFactory();
        _IShipFactory.GetShipper().Ship();
    }
    
    private void btnPurolator_Click(object sender, EventArgs e)
    {
        _IShipFactory = new ShipperPurolatorFactory();
        _IShipFactory.GetShipper().Ship();
    }

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


    Thursday, April 6, 2017 8:30 AM

Answers

All replies

  • Hi Mou_kolkata,

    Refer to your description, this issue is about the C# development. Since our forum is to discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor, I will help you move this thread to this appropriate forum: Visual Studio Languages  >  Visual C# to seek for the professional support for your issue, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 7, 2017 5:26 AM
  • I have used following guide.

    • If the class itself knows how to create instance and no need to call outside resources or dependencies, then I use factory method on class itself. For example Color.FromArgb knows how to create color from values so no need for factory class.
    • If the construction of an instance requires calls to outside resources or dependencies, then I have used separate factory class. Usually this is implemented as abstract factory. Example might be like GPS system where we need to get Route instance based on attributes like weather, road condition, traffic, speed, distance, drive time etc. and the abstract factory class that creates us route would be RouteProvider that has method like CreateRoute that returns constructed Route. Actual implementations might be something like FastestRouteProvider,  ShortestRouteProvider or BestRouteProvider.

    • Edited by MasaSam Friday, April 7, 2017 5:53 AM
    • Proposed as answer by cheong00Editor Friday, April 7, 2017 6:05 AM
    Friday, April 7, 2017 5:52 AM
  • i need to understand when to use factory method pattern and when to use abstract factory pattern.

    if possible explain with scenario that when which one people prefer to develop.

    Thursday, April 13, 2017 8:52 AM
  • Hi Mou,

    Have you look into the below articles, they have explain quite well. Hope this helps you.

    Factory Method Pattern vs. Abstract Factory Pattern

    Factory Method vs Abstract Factory


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Mou_kolkata Thursday, April 13, 2017 12:54 PM
    Thursday, April 13, 2017 9:11 AM
    Moderator
  • It isn't a case of 'which one people to prefer to develop'. It is a case of two slightly different patterns and using the one most suitable.

    As MasaSam says above - a factory method is where a class has a static method capable of producing instances of that class (or, normally, a descendent of that class). A factory class is where you have an entirely separate class whose purposes is to act as a factory capable of producing instances of some other class.

    Thursday, April 13, 2017 9:14 AM
  • you said :- As MasaSam says above - a factory method is where a class has a static method capable of producing instances of that class (or, normally, a descendent of that class).

    which is not clear to me. would u mind to come with a sample example what you said and i highlighted here.

    Thursday, April 13, 2017 12:54 PM