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