Design patterns Static factory method Static Factory method


We can provide a meaningful name for our constructors.

We can provide several constructors with the same number and type of parameters, something that as we saw earlier we can’t do with class constructors.

public class RandomIntGenerator {
    private final int min;
    private final int max;
    private RandomIntGenerator(int min, int max) {
        this.min = min;
        this.max = max;
    public static RandomIntGenerator between(int max, int min) {
        return new RandomIntGenerator(min, max);
    public static RandomIntGenerator biggerThan(int min) {
        return new RandomIntGenerator(min, Integer.MAX_VALUE);
    public static RandomIntGenerator smallerThan(int max) {
        return new RandomIntGenerator(Integer.MIN_VALUE, max);
    public int next() {...}