Factory Design Template - Why Do I Need an Interface?

I began to study various design patterns, and now I am focused on Factory design pattern. I looked at some examples, youtube tuturials and blogs, and I got the most, but I still do not understand why the interface is needed.

Official definition:

Define an interface for creating an object, but let subclasses which class to instantiate. The Factory method allows the class to defer instantiation of subclasses.

So the interface seems to be an important part of the Factory design pattern, but the only reason I found it to be practical when you make a collection in the main method. If you do not want this, you can simply delete it (look at the code below where possible) and it still works as 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 this interface? I read a few abstract explanations, but I did not understand this, so I prefer practical answers.

Thanks in advance!

+6
source share
5 answers

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.

+7
source

You can always apply the design template the way you like. There is nothing wrong with removing an interface. However, you should be aware that removing the interface results in a less general structure.

You use the factory pattern because you do not want your calling code (main function) to know anything about the details of the construct. You do not want to know what the FordFiest is. You just want the car to be built at all. If this is a Ford Fiesta, then so be it. This detail is what the factory (and its subclasses) should handle. Right?

If you do not want your code to depend on a particular factory, you need an additional level of abstraction provided by the interface, which leads to the "decoupling" of the two parts. If you do not pay attention to your code binding to a specific factory implementation, you can always remove the interface.

I hope I helped!

+2
source

Of course, you can use CarFactory to create derived classes of cars, but anyone who wants to support the creation of cars should get it from CarFactory . It seems to me that the purpose of the interface is to provide more flexibility in who can act as a car factory. The concept is that all you have to do to be CarFactory in this example is to implement the ICreateCars interface. You do not need to enter the overhead of the supplied CarFactory base class.

The key is that you decide what type of parameter will be on the functions that take the Car factory. (or what type of member will be for the member that stores CarFactory ). If you use ICreateCars , then anyone who wants to create cars should only implement the interface. If your parameter type is CarFactory , then they would have to inherit from CarFactory .

+2
source

Well, not really - the interface is not an integral part of this template. You could implement a factory as well as a static class or even a regular one.

It's just that using interfaces is usually a very good idea, and the best component-based constructs make good use of it. It is called Interface Based Programming , among its main advantages is that it allows IoC containers and that it makes your code verifiable.

I personally, I use interfaces almost blindly, without even thinking about it ...

+2
source

The purpose of the Factory template is abstract creation. In this case, it will be possible to exchange Factory for the correct implementation.

An abstraction can be implemented by an interface or an abstract class in .NET.

 public interface ICreateCars { Car CreateCar(string color); } 

The result of building ( Car ) can be a class or an interface, just look at the situation. How to build this Factory mission.

0
source

All Articles