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. Factory pattern: issue with this definition "let subclass decide which class to instantiate."

Factory pattern: issue with this definition "let subclass decide which class to instantiate."

Scheduled Pinned Locked Moved C#
questionalgorithmsregexhelp
3 Posts 3 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

    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

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

      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

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

      What you have there is fine. In the GoF (Gang of Four) definition, they are talking about the idea of having something like an abstract base class - the factory method will use this as the return type, but would create concrete versions of the classes that are subclassed off this. As you are using an interface instead (a better choice to my mind), this is your return type.

      This space for rent

      1 Reply Last reply
      0
      • T Tridip Bhattacharjee

        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

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

        Tridip Bhattacharjee wrote:

        please guide me regarding above definition.

        Open this page[^], and scroll down to the answer that begins with "Let's understand difference between Factory and FactoryMethod with an example".

        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