September 2019
Intermediate to advanced
816 pages
18h 47m
English
In a nutshell, the Factory pattern allows us to create several kinds of objects without exposing the instantiation process to the caller. This way, we can hide the complex and/or sensitive process of creating objects and expose an intuitive and easy-to-use factory of objects to the caller.
In a classic implementation, the Factory pattern relies on an intern switch(), as shown in the following example:
public static Fruit newInstance(Class<?> clazz) { switch (clazz.getSimpleName()) { case "Gac": return new Gac(); case "Hemi": return new Hemi(); case "Cantaloupe": return new Cantaloupe(); default: throw new IllegalArgumentException( "Invalid clazz argument: " + clazz); }}
Here, Gac, Hemi, and Cantaloupe ...