Factory Design Pattern - Why Interface necessary?

Viewed 25859

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!

6 Answers

Interfaces let you document that there are a set of classes that honor a specific set of polymorphic methods without having to create an inheritance hierarchy to link all of the classes.

Without Interfaces the designer would have to create an artificial base class that they all inherited from. This becomes impossible when a Class needs to implement two different sets of polymorphic methods.

Multiple inheritance is something the language designers wanted to avoid because it is unclear what methods should be invoked when a class has two parents. Beyond that, artificial inheritance of implementations is always awkward.

Fundamentally, Interfaces let you inherit the method definitions without inheriting any implementation.

Related