Factory Pattern explained

I know there are already millions of blogs explaining this concept.
But still I wanted to make this clear because this is a pattern that all developers should know, but yet ….
So what is actually the Factory Pattern ?
The best definition I found and made the best sense to me was the following:

The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.

So it actually a way of programming where you need objects, but do not have to specify which object you need ?

So how does this pattern actually works, and what do you need to implement this pattern? Three things…

  • A factory
  • An Interface
  • Classes that implement this interface

Basically it comes down to this:

The factory class returns objects/classes who implement  a certain this interface

Let’s look at this with the following example, I need a class that generates for me a transportation vehicle which can be a bike, a car, a boat, an airplane.
So I create an interface:

interface IVehicle
int CountNumberOfWheels();
void DisplayNumberOfWheels();

Then I create two classes who implement this interface:

class Bike : IVehicle
public int CountNumberOfWheels()
return 2;

public void DisplayNumberOfWheels()
Console.WriteLine("This BIKE has'" + CountNumberOfWheels() + "' number of wheels");


class Car : IVehicle
public int CountNumberOfWheels()
return 4;

public void DisplayNumberOfWheels()
Console.WriteLine("This CAR has'" + CountNumberOfWheels() +"' number of wheels");

I create a class to easily determine which vehicle I want, this will be passed to my factory as parameter.

public enum VehicleType

The last thing we need now to implement our Factory Pattern is our ‘factory’ of objects:

public  class VechicleFactory
public static IVehicle GetVehicle(VehicleType vehicleType)
IVehicle vehicle = null;

switch (vehicleType)
case VehicleType.Bike:
return vehicle = new Bike();
case VehicleType.Car:
return vehicle = new Car();

return vehicle;

Notice that my factory actually returns a type of object that implemented interface.
Now I can start using my Factory Pattern:

class Program
static void Main(string[] args)
IVehicle vehicle= VechicleFactory.GetVehicle(VehicleType.Bike);

vehicle = VechicleFactory.GetVehicle(VehicleType.Car);



The output will look like this:

2018-06-05 08_32_06-c__users_je35608_source_repos_SBS.FactoryPattern_SBS.FactoryPatternExplained_bin

So to implement this type of design pattern we need three things:

  • An interface
  • A class that implements that interface
  • A factory that generates objects/classes based on that interface

But every object has his own interpretation of the methods of the interface.
If we would not use this pattern, and place this code for instance in one class called vehicle we would needs to use switch and if statement if we want to do stuff for a specific vehicle.
Further does pattern allow you do easily generate more vehicles in the future without having to modify the other classes.


Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )


Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )


Verbinden met %s