Creating objects based on input with the factory method pattern
The primary purpose of this pattern is to centralize the class’s instantiation of a specific type. The pattern leaves the decision to create the exact class type up to the client at runtime. The factory method design pattern was described in the GoF’s book.
Motivation
The factory method pattern enforces the separation of code and its responsibility for creating new instances of the class, that is, such a method provides the expected result. The factory hides an application class hierarchy based on a generics abstraction and introduces a common interface. It transparently separates the instantiation logic from the rest of the code. By introducing the common interface, the client gains the freedom to decide on a particular class instance at runtime.
The pattern is often used in the early stages of an application because it is simple to refactor and provides a high level of clarity.
Although this...