Steve Armstrong Steve Armstrong - 4 months ago 6x
Java Question

Managing constructors with many parameters in Java

In some of our projects, there's an class hierarchy that adds more parameters as it goes down the chain. At the bottom, some of the classes can have up to 30 parameters, 28 of which are just being passed into the super constructor.

I'll acknowledge that using automated DI through something like Guice would be nice, but because of some technical reasons, these specific projects are constrained to Java.

A convention of organizing the arguments alphabetically by type doesn't work because if a type is refactored (the Circle you were passing in for argument 2 is now a Shape) it can suddenly be out of order.

This question might be to specific and fraught with "If that's your problem, you're doing it wrong at a design level" criticisms, but I'm just looking for any viewpoints.


The Builder Design Pattern might help. Consider the following example

public class StudentBuilder
    private String _name;
    private int _age = 14;      // this has a default
    private String _motto = ""; // most students don't have one

    public StudentBuilder() { }

    public Student buildStudent()
        return new Student(_name, _age, _motto);

    public StudentBuilder name(String _name)
        this._name = _name;
        return this;

    public StudentBuilder age(int _age)
        this._age = _age;
        return this;

    public StudentBuilder motto(String _motto)
        this._motto = _motto;
        return this;

This lets us write code like

Student s1 = new StudentBuilder().name("Eli").buildStudent();
Student s2 = new StudentBuilder()
                 .motto("Aloha, Mr Hand")

If we leave off a required field (presumably name is required) then we can have the Student constructor throw an exception. And it lets us have default/optional arguments without needing to keep track of any kind of argument order, since any order of those calls will work equally well.