The factory method pattern

The factory method pattern concerns defining an interface (or abstract class) method for creating dependency objects. This method is called a factory method. The class (or interface) that holds the factory method will be considered an abstract creator. The actual object creation process does not happen in the factory method directly. 

The concrete creators (which implement the factory method) will decide which dependent class to instantiate. In short, the dependent object is decided at runtime. This process has been described in the following diagram:

The factory pattern's implementation is a four-step process:

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.