Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C#: When one should go for factory method pattern instead of factory pattern

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

Scheduled Pinned Locked Moved C#
csharpdesignregexarchitecture
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    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
    
    P L 2 Replies Last reply
    0
    • T Tridip Bhattacharjee

      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
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Some reading for you. factory method pattern vs factory pattern - Google Search[^]

      This space for rent

      OriginalGriffO 1 Reply Last reply
      0
      • P Pete OHanlon

        Some reading for you. factory method pattern vs factory pattern - Google Search[^]

        This space for rent

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        He doesn't like reading: it makes it difficult to be a Help Vampire. :sigh:

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P L 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          He doesn't like reading: it makes it difficult to be a Help Vampire. :sigh:

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          It's like a stake through the heart.

          This space for rent

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            He doesn't like reading: it makes it difficult to be a Help Vampire. :sigh:

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Quote:

            He doesn't like reading

            Yeah, and that is exactly why a few days ago I failed to understand his thread on hashing/encryption collected 30+ replies. :confused:

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            P OriginalGriffO 2 Replies Last reply
            0
            • L Luc Pattyn

              Quote:

              He doesn't like reading

              Yeah, and that is exactly why a few days ago I failed to understand his thread on hashing/encryption collected 30+ replies. :confused:

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              It surprised me as well. Granted, I did reply - but that was to warn people that he was a Help Vampire.

              This space for rent

              1 Reply Last reply
              0
              • L Luc Pattyn

                Quote:

                He doesn't like reading

                Yeah, and that is exactly why a few days ago I failed to understand his thread on hashing/encryption collected 30+ replies. :confused:

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                In our collective defence, most of 'em were to Gerry - who was trying to defend the indefensible without knowing anything about what he was saying. :-D

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                L 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  In our collective defence, most of 'em were to Gerry - who was trying to defend the indefensible without knowing anything about what he was saying. :-D

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  That is alright then :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  1 Reply Last reply
                  0
                  • T Tridip Bhattacharjee

                    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
                    
                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Tridip Bhattacharjee wrote:

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

                    IDbConnection.CreateCommand(); Doesn't even take parameters :)

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups