static factory pattern

By | September 29, 2020
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

static factory method over constructors:
1. Constructor don’t have meaningful names, but static factory method has
2. Static factory method returns same type, subtype, primitives
3. Static factory could return same instance if needed

Sample of static factory method:

String value1 = String.valueOf(1);
String value2 = String.valueOf(1.0L);
Optional<String> value1 = Optional.empty();
Arrays.asList(1, 2, 3);

link