Factory Pattern Question With Generics
-
Ok i am struggling with the factory pattern big time. This pattern seems great if you are creating objects that share all the same properties but i can never find a sample client that uses the specific concrete classes that have different properteries. ( Ok that probably made no sense so here is a sample using generics ) public abstract class Vehicle { protected string name; protected string type; public Vehicle() { } public Vehicle(string name) { this.name = name; } public string Name { get { return name; } set { name = value; } } public string Type { get { return type; } } } // a concrete vehicle public class Car : Vehicle { public Car() : base() { } public Car(string name, string type) : base(name) { base.type = type; } } // another concrete vehicle public class Truck : Vehicle { bool 4WD = false; public Truck() : base() { } public Truck(string name, string type) : base(name) { base.type = type; } public Engage4WD() // This only exists for a truck { 4WD = true; } } interface IVehicleFactory { T CreateObject(string name) where T : Vehicle, new(); } public class VehicleFactory : IVehicleFactory { public T CreateObject(string name) where T : Vehicle, new() { // create instance of Type T where this type derives from Vehicle T v = new T(); v.Name = name; return v; } } public class FactoryPatternUsingGenericMethod { static void Main(string[] args) { IVehicleFactory f = new VehicleFactory(); Car c = f.CreateObject("Kitty"); Truck d = f.CreateObject("test"); } } } It appears at this point i can get a truck or a car using the factory however what is the benefit of this. This just seems like alot of work when i could have just had a separate truck object and car object inheriting from vehicle and use the new operator without resorting to a factory. Would this not give the same result. I guess i just can't seem to qu
-
Ok i am struggling with the factory pattern big time. This pattern seems great if you are creating objects that share all the same properties but i can never find a sample client that uses the specific concrete classes that have different properteries. ( Ok that probably made no sense so here is a sample using generics ) public abstract class Vehicle { protected string name; protected string type; public Vehicle() { } public Vehicle(string name) { this.name = name; } public string Name { get { return name; } set { name = value; } } public string Type { get { return type; } } } // a concrete vehicle public class Car : Vehicle { public Car() : base() { } public Car(string name, string type) : base(name) { base.type = type; } } // another concrete vehicle public class Truck : Vehicle { bool 4WD = false; public Truck() : base() { } public Truck(string name, string type) : base(name) { base.type = type; } public Engage4WD() // This only exists for a truck { 4WD = true; } } interface IVehicleFactory { T CreateObject(string name) where T : Vehicle, new(); } public class VehicleFactory : IVehicleFactory { public T CreateObject(string name) where T : Vehicle, new() { // create instance of Type T where this type derives from Vehicle T v = new T(); v.Name = name; return v; } } public class FactoryPatternUsingGenericMethod { static void Main(string[] args) { IVehicleFactory f = new VehicleFactory(); Car c = f.CreateObject("Kitty"); Truck d = f.CreateObject("test"); } } } It appears at this point i can get a truck or a car using the factory however what is the benefit of this. This just seems like alot of work when i could have just had a separate truck object and car object inheriting from vehicle and use the new operator without resorting to a factory. Would this not give the same result. I guess i just can't seem to qu
The benefit is that you'll have a single point that creates instances of a common base class or interface based on a parameter. Your class factory should *never* return a Car or a Truck, it should return only Vehicle. You may argue that this would stop you from using the Engage4WD method from your Truck class. This is because your class inheritance is wrong. A Truck should not derive from a Vehicle, because it does not complies with the Liskov Substitution Principle. It seems that you're using the flawed "is a" principle for OO design, so, if you don't know LSP yet, please read this document[^] and understand why a Square should not derive from a Rectangle. After reading it, you'll understand why your class factory doesn't help in anything and should return always a base class. I see dead pixels Yes, even I am blogging now!
-
The benefit is that you'll have a single point that creates instances of a common base class or interface based on a parameter. Your class factory should *never* return a Car or a Truck, it should return only Vehicle. You may argue that this would stop you from using the Engage4WD method from your Truck class. This is because your class inheritance is wrong. A Truck should not derive from a Vehicle, because it does not complies with the Liskov Substitution Principle. It seems that you're using the flawed "is a" principle for OO design, so, if you don't know LSP yet, please read this document[^] and understand why a Square should not derive from a Rectangle. After reading it, you'll understand why your class factory doesn't help in anything and should return always a base class. I see dead pixels Yes, even I am blogging now!
never heard of the principle, looks like i have some homework. I figured something was wrong, the objects are alike but not enough to work correctly with the inheritance. I'll continue looking at lsp to figure out how i would fix the sample i wrote. It just seems like based on what i've always been told truck would inherit from vehicle because it does share some similar properties. Thanks
-
The benefit is that you'll have a single point that creates instances of a common base class or interface based on a parameter. Your class factory should *never* return a Car or a Truck, it should return only Vehicle. You may argue that this would stop you from using the Engage4WD method from your Truck class. This is because your class inheritance is wrong. A Truck should not derive from a Vehicle, because it does not complies with the Liskov Substitution Principle. It seems that you're using the flawed "is a" principle for OO design, so, if you don't know LSP yet, please read this document[^] and understand why a Square should not derive from a Rectangle. After reading it, you'll understand why your class factory doesn't help in anything and should return always a base class. I see dead pixels Yes, even I am blogging now!
Daniel Turini wrote:
please read this document[^]
That was a really good read.:cool: Regards Senthil _____________________________ My Blog | My Articles | WinMacro