Click here to Skip to main content
15,887,886 members
Home / Discussions / C#
   

C#

 
GeneralRe: C#: When one should go for factory method pattern instead of factory pattern Pin
OriginalGriff5-Apr-17 6:00
mveOriginalGriff5-Apr-17 6:00 
GeneralRe: C#: When one should go for factory method pattern instead of factory pattern Pin
Pete O'Hanlon5-Apr-17 7:33
mvePete O'Hanlon5-Apr-17 7:33 
GeneralRe: C#: When one should go for factory method pattern instead of factory pattern Pin
Luc Pattyn5-Apr-17 8:18
sitebuilderLuc Pattyn5-Apr-17 8:18 
GeneralRe: C#: When one should go for factory method pattern instead of factory pattern Pin
Pete O'Hanlon5-Apr-17 8:33
mvePete O'Hanlon5-Apr-17 8:33 
GeneralRe: C#: When one should go for factory method pattern instead of factory pattern Pin
OriginalGriff5-Apr-17 8:37
mveOriginalGriff5-Apr-17 8:37 
GeneralRe: C#: When one should go for factory method pattern instead of factory pattern Pin
Luc Pattyn5-Apr-17 9:02
sitebuilderLuc Pattyn5-Apr-17 9:02 
AnswerRe: C#: When one should go for factory method pattern instead of factory pattern Pin
Eddy Vluggen6-Apr-17 1:48
professionalEddy Vluggen6-Apr-17 1:48 
QuestionFactory pattern: issue with this definition "let subclass decide which class to instantiate." Pin
Tridip Bhattacharjee5-Apr-17 2:09
professionalTridip Bhattacharjee5-Apr-17 2:09 
after searching google for Factory pattern definition i found it is

Factory pattern

In Factory pattern, we create object without exposing the creation logic. In this pattern,
an interface is used for creating an object, but let subclass decide which class to instantiate.
The creation of object is done when it is required.
The Factory method allows a class later instantiation to subclasses.

see my Factory pattern related code and there is no subclass which instantiate class object rather i have class with a static function which create & return class object.

so my question is that above definition is saying "subclass decide which class to instantiate" but in my code there is no subclass or child class rather a separate class create instance. is the above definition is for factory method pattern ?

here is my code
C#
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
C#
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();
        }

please guide me regarding above definition.

thanks
tbhattacharjee

AnswerRe: Factory pattern: issue with this definition "let subclass decide which class to instantiate." Pin
Pete O'Hanlon5-Apr-17 2:34
mvePete O'Hanlon5-Apr-17 2:34 
AnswerRe: Factory pattern: issue with this definition "let subclass decide which class to instantiate." Pin
Eddy Vluggen5-Apr-17 2:54
professionalEddy Vluggen5-Apr-17 2:54 
QuestionTask.Wait() -Why?? Pin
Kevin Marois4-Apr-17 6:54
professionalKevin Marois4-Apr-17 6:54 
AnswerRe: Task.Wait() -Why?? Pin
Pete O'Hanlon4-Apr-17 7:00
mvePete O'Hanlon4-Apr-17 7:00 
GeneralRe: Task.Wait() -Why?? Pin
Kevin Marois4-Apr-17 7:19
professionalKevin Marois4-Apr-17 7:19 
GeneralRe: Task.Wait() -Why?? Pin
Pete O'Hanlon4-Apr-17 7:26
mvePete O'Hanlon4-Apr-17 7:26 
GeneralRe: Task.Wait() -Why?? Pin
Richard Deeming4-Apr-17 7:38
mveRichard Deeming4-Apr-17 7:38 
GeneralRe: Task.Wait() -Why?? Pin
Pete O'Hanlon4-Apr-17 7:40
mvePete O'Hanlon4-Apr-17 7:40 
AnswerRe: Task.Wait() -Why?? Pin
Richard Deeming4-Apr-17 7:48
mveRichard Deeming4-Apr-17 7:48 
GeneralRe: Task.Wait() -Why?? Pin
Kevin Marois4-Apr-17 7:52
professionalKevin Marois4-Apr-17 7:52 
GeneralRe: Task.Wait() -Why?? Pin
Richard Deeming4-Apr-17 8:04
mveRichard Deeming4-Apr-17 8:04 
GeneralRe: Task.Wait() -Why?? Pin
Tridip Bhattacharjee5-Apr-17 23:30
professionalTridip Bhattacharjee5-Apr-17 23:30 
GeneralRe: Task.Wait() -Why?? Pin
Richard Deeming6-Apr-17 1:30
mveRichard Deeming6-Apr-17 1:30 
Questionin which scenario people use factory design patter Pin
Tridip Bhattacharjee4-Apr-17 1:58
professionalTridip Bhattacharjee4-Apr-17 1:58 
AnswerRe: in which scenario people use factory design patter Pin
Eddy Vluggen4-Apr-17 2:29
professionalEddy Vluggen4-Apr-17 2:29 
GeneralRe: in which scenario people use factory design patter Pin
Tridip Bhattacharjee4-Apr-17 3:48
professionalTridip Bhattacharjee4-Apr-17 3:48 
GeneralRe: in which scenario people use factory design patter Pin
Pete O'Hanlon4-Apr-17 4:14
mvePete O'Hanlon4-Apr-17 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.