The Power of Constructors
The preceding 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:
new SendMoneyCommandBuilder()
.sourceAccountId(new AccountId(41L))
.targetAccountId(new AccountId(42L))
// ... initialize many other fields
.build();
We could...