C#: When one should go for factory method pattern instead of factory pattern
-
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
-
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
Some reading for you. factory method pattern vs factory pattern - Google Search[^]
This space for rent
-
Some reading for you. factory method pattern vs factory pattern - Google Search[^]
This space for rent
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...
-
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...
It's like a stake through the heart.
This space for rent
-
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...
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
-
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
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
-
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
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...
-
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...
That is alright then :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
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
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)