Design Principle #2: Consider using the builder pattern when there are many constructor arguments

Before jumping on to builder pattern, let's talk about telescoping constructor (anti)-pattern and JavaBeans pattern. In Java, it is not possible to set default values for constructor parameters. This is where the telescoping constructor pattern comes handy. A class has multiple constructors, where each constructor calls a more specific constructor in the hierarchy, which has … Continue reading Design Principle #2: Consider using the builder pattern when there are many constructor arguments

Design Principle #1: Consider static factory method pattern instead of constructors

In some scenarios, instead of having a constructor, it is better to have a public static factory method that returns an instance of that class. For example, the below method returns an instance of the Boolean object when given boolean primitive type. public static Boolean valueOf(boolean val) { return b?Boolean.TRUE:Boolean.FALSE; } This method has the … Continue reading Design Principle #1: Consider static factory method pattern instead of constructors