Abstract factory
-
Hi! Can somebody explain to me why we use a Base interface as a return type in a Factory interface? Second question in concerned with the code in Main. I figured out that Factory factory = new ConcreteFactory2() is a reference to the implementation of a ConcreteFactory2 of GetObject() method. What is the next line? Thanks. I appreciate your help! abstractfactory { class Program { static void Main(string[] args) { Factory factory = new ConcreteFactory2(); Base obj = factory.GetObject(); obj.DoIt(); } } } interface Factory { Base GetObject(); } class ConcreteFactory1 :Factory { public Base GetObject() { return new Derived1(); // do we return an instance of derived 1 interface? } } class ConcreteFactory2 : Factory { public Base GetObject() { return new Derived2(); } } interface Base { void DoIt(); } class Derived1 : Base { public void DoIt() { Console .WriteLine("Derived 1 method"); } } class Derived2 : Base { public void DoIt() { Console.WriteLine("Derived 2 method"); } }
-
Hi! Can somebody explain to me why we use a Base interface as a return type in a Factory interface? Second question in concerned with the code in Main. I figured out that Factory factory = new ConcreteFactory2() is a reference to the implementation of a ConcreteFactory2 of GetObject() method. What is the next line? Thanks. I appreciate your help! abstractfactory { class Program { static void Main(string[] args) { Factory factory = new ConcreteFactory2(); Base obj = factory.GetObject(); obj.DoIt(); } } } interface Factory { Base GetObject(); } class ConcreteFactory1 :Factory { public Base GetObject() { return new Derived1(); // do we return an instance of derived 1 interface? } } class ConcreteFactory2 : Factory { public Base GetObject() { return new Derived2(); } } interface Base { void DoIt(); } class Derived1 : Base { public void DoIt() { Console .WriteLine("Derived 1 method"); } } class Derived2 : Base { public void DoIt() { Console.WriteLine("Derived 2 method"); } }
-
Hi! Can somebody explain to me why we use a Base interface as a return type in a Factory interface? Second question in concerned with the code in Main. I figured out that Factory factory = new ConcreteFactory2() is a reference to the implementation of a ConcreteFactory2 of GetObject() method. What is the next line? Thanks. I appreciate your help! abstractfactory { class Program { static void Main(string[] args) { Factory factory = new ConcreteFactory2(); Base obj = factory.GetObject(); obj.DoIt(); } } } interface Factory { Base GetObject(); } class ConcreteFactory1 :Factory { public Base GetObject() { return new Derived1(); // do we return an instance of derived 1 interface? } } class ConcreteFactory2 : Factory { public Base GetObject() { return new Derived2(); } } interface Base { void DoIt(); } class Derived1 : Base { public void DoIt() { Console .WriteLine("Derived 1 method"); } } class Derived2 : Base { public void DoIt() { Console.WriteLine("Derived 2 method"); } }
The abstract factory pattern is explained fairly well here: http://en.wikipedia.org/wiki/Abstract_factory_pattern[^] It returns Base as the object type because different factories will return different types of object, but those objects must have something in common. Imagine a VehicleFactory interface with a method GetVehicle that returns a Vehicle. One type of VehicleFactory might be a PetrolVehicleFactory, that returns vehicles that run on petrol. Another might be an ElectricVehicleFactory that returns electric-powered vehicles. Both of these return type Vehicle, so I can use the factories interchangeably without having to worry about whether it is an ElectricVehicle or a PetrolVehicle that is being handed to me.
VehicleFactory myFactory = new PetrolVehicleFactory();
Vehicle myVehicle = myFactory.GetVehicle();
myVehicle.Start();
myVehicle.DriveOffIntoTheSunset();I can swap PetrolVehicleFactory for ElectricVehicleFactory, and nothing else in my application needs to be changed:
VehicleFactory myFactory = new ElectricVehicleFactory();
Vehicle myVehicle = myFactory.GetVehicle();
myVehicle.Start();
myVehicle.DriveOffIntoTheSunset();As long as ElectricVehicle and PetrolVehicle both do what a Vehicle is supposed to do, I don't care what exact type of Vehicle the factory gives me. I just want a Vehicle so that I can drive off into the sunset. It's the factory's responsibility to know what type of Vehicle to make and how to make it.
-
The abstract factory pattern is explained fairly well here: http://en.wikipedia.org/wiki/Abstract_factory_pattern[^] It returns Base as the object type because different factories will return different types of object, but those objects must have something in common. Imagine a VehicleFactory interface with a method GetVehicle that returns a Vehicle. One type of VehicleFactory might be a PetrolVehicleFactory, that returns vehicles that run on petrol. Another might be an ElectricVehicleFactory that returns electric-powered vehicles. Both of these return type Vehicle, so I can use the factories interchangeably without having to worry about whether it is an ElectricVehicle or a PetrolVehicle that is being handed to me.
VehicleFactory myFactory = new PetrolVehicleFactory();
Vehicle myVehicle = myFactory.GetVehicle();
myVehicle.Start();
myVehicle.DriveOffIntoTheSunset();I can swap PetrolVehicleFactory for ElectricVehicleFactory, and nothing else in my application needs to be changed:
VehicleFactory myFactory = new ElectricVehicleFactory();
Vehicle myVehicle = myFactory.GetVehicle();
myVehicle.Start();
myVehicle.DriveOffIntoTheSunset();As long as ElectricVehicle and PetrolVehicle both do what a Vehicle is supposed to do, I don't care what exact type of Vehicle the factory gives me. I just want a Vehicle so that I can drive off into the sunset. It's the factory's responsibility to know what type of Vehicle to make and how to make it.
-
The abstract factory pattern is explained fairly well here: http://en.wikipedia.org/wiki/Abstract_factory_pattern[^] It returns Base as the object type because different factories will return different types of object, but those objects must have something in common. Imagine a VehicleFactory interface with a method GetVehicle that returns a Vehicle. One type of VehicleFactory might be a PetrolVehicleFactory, that returns vehicles that run on petrol. Another might be an ElectricVehicleFactory that returns electric-powered vehicles. Both of these return type Vehicle, so I can use the factories interchangeably without having to worry about whether it is an ElectricVehicle or a PetrolVehicle that is being handed to me.
VehicleFactory myFactory = new PetrolVehicleFactory();
Vehicle myVehicle = myFactory.GetVehicle();
myVehicle.Start();
myVehicle.DriveOffIntoTheSunset();I can swap PetrolVehicleFactory for ElectricVehicleFactory, and nothing else in my application needs to be changed:
VehicleFactory myFactory = new ElectricVehicleFactory();
Vehicle myVehicle = myFactory.GetVehicle();
myVehicle.Start();
myVehicle.DriveOffIntoTheSunset();As long as ElectricVehicle and PetrolVehicle both do what a Vehicle is supposed to do, I don't care what exact type of Vehicle the factory gives me. I just want a Vehicle so that I can drive off into the sunset. It's the factory's responsibility to know what type of Vehicle to make and how to make it.
-
Thank you for your explanation. It's a lot clearer. Could you just explain the client code? I mean is Vehicle myVehicle = myFactory.GetVehicle(); a reference to GetVehicle? Thanks
-
The variable
myVehicle
holds a reference to theVehicle
object returned from theGetVehicle()
method.