Ive been thinking about this and I've seen different examples on this. Usually I would chain constructors in a upward manner. So if I have 4 different constructors, starting with a default constructor, who calls the constructor where we need to pass in one argument, who calls the constructor where we need 2 arguments to pass in and so on, so we have usable objects, where all the fields hold data.
Is there a different approach to chaining constructor because I couldn't think of another case ? Why would I want my constructor, where I need 3 arguments to pass in, call my constructor where I only need to pass in 2 arguments ?
The Builder pattern works for this scenario. A rough demo could look like this:
Each set method returns a reference to the
DogBuilderinstance, with that property set. Calling.build()calls a private/package private constructor with all values set.This is a good article for reference: https://dzone.com/articles/design-patterns-the-builder-pattern