What is the Factory Design Pattern?

The Factory Design Pattern is a creational design pattern.

The Factory Design Pattern is one of the most commonly used and useful design patterns and it is essential that you master its implementation. This design pattern falls under the category of creational design patterns. Creational design patterns are used to instantiate objects.

The Factory Design essentially abstracts away the details of object creation and provides a reference to the object through a common interface.

The first step to utilizing the factory design pattern is design an interface, such as the one below for a Car.

public interface Car
   /** Get the make of the car. */
   public String getMake();
   /** Get the model of the car. */
   public String getModel();

   /** Get the year of the car. */
   public int getYear();

The second step is define several classes which implement the interface.

The final step is to put together a factory which is responsible for building the Car objects.