Factory Method

Factory Method

In factory method, we define an interface for creating an object, but let sub classes decide which class to instantiate. Factory Method lets a class defer instantiation to sub classes.

That's the definition, but what it really means to develop it?

Define a runtime interface for creating an object. It’s called a factory because it creates various types of objects without necessarily knowing what kind of object it creates or how to create it.

Let me put it this way, we create object without exposing the creation logic to client and the client use the same common interface to create new type of object.

Below example should make it clear -

Both the clients (Germany and USA) uses same interfaces to create their required types of car.