Skip to main content

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()
                 .name("Spicoli")
                 .age(16)
                 .motto("Aloha, Mr Hand")
                 .buildStudent();

If we leave off a required field (presumably name is required) then we can have the Student constructor will throw an exception when a required parameter is null. And 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.

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()
                 .name("Spicoli")
                 .age(16)
                 .motto("Aloha, Mr Hand")
                 .buildStudent();

If we leave off a required field (presumably name is required) then we can have the Student constructor will throw an exception when a required parameter is null. 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.

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()
                 .name("Spicoli")
                 .age(16)
                 .motto("Aloha, Mr Hand")
                 .buildStudent();

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.

fixed typo
Source Link
Eli Courtwright
  • 195.3k
  • 69
  • 224
  • 257

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;_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()
                 .name("Spicoli")
                 .age(16)
                 .motto("Aloha, Mr Hand")
                 .buildStudent();

If we leave off a required field (presumably name is required) then we can have the Student constructor will throw an exception when a required parameter is null. 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.

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()
                 .name("Spicoli")
                 .age(16)
                 .motto("Aloha, Mr Hand")
                 .buildStudent();

If we leave off a required field (presumably name is required) then we can have the Student constructor will throw an exception when a required parameter is null. 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.

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()
                 .name("Spicoli")
                 .age(16)
                 .motto("Aloha, Mr Hand")
                 .buildStudent();

If we leave off a required field (presumably name is required) then we can have the Student constructor will throw an exception when a required parameter is null. 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.

Source Link
Eli Courtwright
  • 195.3k
  • 69
  • 224
  • 257

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()
                 .name("Spicoli")
                 .age(16)
                 .motto("Aloha, Mr Hand")
                 .buildStudent();

If we leave off a required field (presumably name is required) then we can have the Student constructor will throw an exception when a required parameter is null. 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.