Anti Pattern: Telescoping constructor pattern

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

It lists all constructors. This is an anti pattern. Avoid using it.

class Person {
    String firstName;
    String lastName;
    int age;
    public Person() {}

    public Person(String firstName) {
        this.firstName = firstName;
    }

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Person(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

}