An interface (or an abstract factory base class that essentially matches the current interface) is useful whenever the calling factory does not know the type of factory.
You have provided the basis for your own practical example, so I will add here my explanation of why this is not only useful if you have a list of plants:
Imagine a method that a car should create when necessary, not knowing what type of car needs to be created (which is decided by the factory implementation). The method considers the Person object that has the OwnsCar property, and this property ultimately decides whether to call the factory method:
public Car CreateCarForPerson(Person person, ICreateCars carType) { if (person.OwnsCar) { return carType.CreateCar("red"); } else { return null; } }
In the same way, you can 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 that neither of these two methods knows what type of car is being created; they use a factory from which they know the interface, but not the exact type.
So, if you want to provide various factory implementations, you can declare a common interface for your factories. If your factory serves so that your subscribers do not hide the calls to the target constructor, you do not need a factory interface.
source share