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.