Suppose I have a constructor for an object that takes about 6 parameters, but two or more of the parameters are related, for example:
LoanProduct(CurrencySum maxSum, CurrencySum minSum, Interest interest, etc)
CurrencySum is a wrapper for two objects: a BigDecimal and an Enum representing currencies.
Interest is an interface with two sublasses: FixedInterest and VariableInterest.
VariableInterest wraps a BigDecimal representing a fixed value and an VarIndex object that is the varying bit of the interest – VarIndex is basically libor 1m, libor 2m, etc (VariableInterest = BigDecimal fixedValue + VarIndex variableValue)
Now, the bit that annoys me is that when a LoanProduct is created, I have to check in the constructor if maxSum and minSum are of the same Currency, and if VariableInterest holds a VarIndex corresponding to that Currency, as a LoanProduct in euros can’t have a VarIndex set for libor, it has to be euribor.
Is there an elegant way to construct the LoanProduct without all this checking ? I’m considering using the builder pattern and make all the checks in the builder, but it still seems ugly.
Builder pattern in my opinion. Effective Java has a good chapter on this.
Or you could encapsulate some of these parameters in their own objects to reduce the number