Reader small image

You're reading from  Get Your Hands Dirty on Clean Architecture - Second Edition

Product typeBook
Published inJul 2023
PublisherPackt
ISBN-139781805128373
Edition2nd Edition
Right arrow
Author (1)
Tom Hombergs
Tom Hombergs
author image
Tom Hombergs

Tom Hombergs is a software engineer by profession and by passion with more than a decade of experience working on many different software projects for many different clients across various industries. In software projects, he takes on the roles of software developer, architect, and coach, with a focus on the Java ecosystem. He has found that writing is the best way to learn, so he likes to dive deep into topics he encounters in his software projects to create texts that give structure to the chaotic world of software development. He regularly writes about software development on his blog and is an occasional speaker at conferences.
Read more about Tom Hombergs

Right arrow

The power of constructors

Our input model, SendMoneyCommand, puts a lot of responsibility on its constructor. Since the class is immutable, the constructor’s argument list contains a parameter for each attribute of the class. And since the constructor also validates the parameters, it’s not possible to create an object with an invalid state.

In our case, the constructor has only three parameters. What if we had more parameters? Couldn’t we use the builder pattern to make it more convenient to use? We could make the constructor with the long parameter list private and hide the call to it in the build() method of our builder. Then, instead of having to call a constructor with 20 parameters, we could build an object like this:

We can still let our constructor do the validation so that the builder cannot construct an object with an invalid state.

Sound good? Think about what happens if we have to add another field to SendMoneyCommandBuilder...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Get Your Hands Dirty on Clean Architecture - Second Edition
Published in: Jul 2023Publisher: PacktISBN-13: 9781805128373

Author (1)

author image
Tom Hombergs

Tom Hombergs is a software engineer by profession and by passion with more than a decade of experience working on many different software projects for many different clients across various industries. In software projects, he takes on the roles of software developer, architect, and coach, with a focus on the Java ecosystem. He has found that writing is the best way to learn, so he likes to dive deep into topics he encounters in his software projects to create texts that give structure to the chaotic world of software development. He regularly writes about software development on his blog and is an occasional speaker at conferences.
Read more about Tom Hombergs