I started looking at different design patterns, and now I am focussing on the Factory Design Pattern. I looked at some examples, youtube tuturials and blogs and I got most but I still didnt get why an interface is necessary.
The official definition is:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
So an Interface seems to be an important part of the Factory Design Pattern, but the only reason I found where its practical is when you make a collection in the main method. If you dont want that, you can just delete it (look at the code below, where that is possible) and it still works like planned.
using System;
using System.Collections.Generic;
using System.Collections;
namespace FactoryDesignPattern
{
class Program
{
static void Main(string[] args)
{
var FordFiestaFactory = new FordFiestaFactory();
var FordFiesta = FordFiestaFactory.CreateCar("Blue");
Console.WriteLine("Brand: {0} \nModel: {1} \nColor: {2}", FordFiesta.Make, FordFiesta.Model, FordFiesta.Color);
Console.WriteLine();
//Inserted this later. Using a collection requires the Interface to be there.
List<ICreateCars> Cars = new List<ICreateCars>();
Cars.Add(new FordFiestaFactory());
Cars.Add(new BMWX5Factory());
foreach (var Car in Cars)
{
var ProductCar = Car.CreateCar("Red");
Console.WriteLine("Brand: {0} \nModel: {1} \nColor: {2}", ProductCar.Make, ProductCar.Model, ProductCar.Color);
Console.WriteLine();
}
Console.ReadKey();
}
}
public abstract class Car
{
public string Make { get; set; }
public string Model { get; set; }
public string EngineSize { get; set; }
public string Color { get; set; }
}
public class FordFiesta : Car
{
public FordFiesta()
{
Make = "Ford";
Model = "Fiesta";
EngineSize = "1.1";
}
}
public class BMWX5 : Car
{
public BMWX5()
{
Make = "BMW";
Model = "X5";
EngineSize = "2.1";
}
}
public interface ICreateCars
{
Car CreateCar(string color);
}
class FordFiestaFactory : ICreateCars
{
public Car CreateCar(string color)
{
return new FordFiesta() { Color = color };
}
}
class BMWX5Factory : ICreateCars
{
public Car CreateCar(string color)
{
return new BMWX5(){ Color = color };
}
}
}
So why do I need that Interface? I read multiple abstract explenations but I didnt get it, so I prefer practical answers.
Thanks in advance!
The interface (or abstract factory base class, which is essentially the same as an interface in effect) is useful whenever the caller of the factory does not know the type of the factory.
You provided the base for your own practical example, so I'll add my explanation here why that's not only useful when having a list of factories:
Imagine a method that is supposed to create a car when appropriate, without knowing what type of car to create (that is decided by the factory implementation). The method looks at a Person
object, which has an OwnsCar
property, and that property ultimately decides whether the factory method should be called:
public Car CreateCarForPerson(Person person, ICreateCars carType)
{
if (person.OwnsCar) {
return carType.CreateCar("red");
} else {
return null;
}
}
In the same way, you could also use such a factory to create an arbitrary number of cars:
public Car[] CreateAnyNumberOfCars(ICreateCars carType)
{
var result = new List<Car>();
for (int i = new Random().Next(100); i >= 0; i--) {
result.Add(carType.CreateCar("blue"));
}
return result.ToArray();
}
Note how none of these two methods knows what car type is being created; they use a factory of whom they only know the interface, but not the exact type.
So, if you want to be able to supply different factory implementations, you can declare a common interface for your factories. If your factory only serves to keep your callers away from direct invocations of the target constructor, you do not need the factory interface.