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

C#

 
AnswerRe: using k means heart diseases prediction system Pin
Dave Kreskowiak7-Apr-17 8:23
mveDave Kreskowiak7-Apr-17 8:23 
GeneralRe: using k means heart diseases prediction system Pin
harold aptroot7-Apr-17 11:04
harold aptroot7-Apr-17 11:04 
QuestionTask Question Pin
Kevin Marois6-Apr-17 8:08
professionalKevin Marois6-Apr-17 8:08 
AnswerRe: Task Question Pin
Pete O'Hanlon6-Apr-17 8:59
mvePete O'Hanlon6-Apr-17 8:59 
GeneralRe: Task Question Pin
Kevin Marois6-Apr-17 9:07
professionalKevin Marois6-Apr-17 9:07 
AnswerRe: Task Question Pin
Eddy Vluggen6-Apr-17 9:02
professionalEddy Vluggen6-Apr-17 9:02 
AnswerRe: Task Question Pin
Richard Deeming7-Apr-17 0:42
mveRichard Deeming7-Apr-17 0:42 
QuestionC#: When one should go for factory method pattern instead of factory pattern Pin
Tridip Bhattacharjee5-Apr-17 4:56
professionalTridip Bhattacharjee5-Apr-17 4:56 
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
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();
        }

now same thing done by factory method pattern where i have to write more code to get the job done
C#
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
C#
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. thanks
tbhattacharjee

AnswerRe: C#: When one should go for factory method pattern instead of factory pattern Pin
Pete O'Hanlon5-Apr-17 5:08
mvePete O'Hanlon5-Apr-17 5:08 
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 
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 

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.