The main problem with a large list of parameters is the readability and the danger that you will mix parameters. You can solve these problems using the Template Builder , as described in Effective Java . This makes the code more readable (especially languages โโthat do not support named and optional parameters):
public class AddressBuilder { private Point _point; private String _houseNumber; // other parameters public AddressBuilder() { } public AddressBuilder WithPoint(Point point) { _point = point; return this; } public AddressBuilder WithHouseNumber(String houseNumber) { _houseNumber = houseNumber; return this; } public Address Build() { return new Address(_point, _houseNumber, ...); } } Address address = new AddressBuilder() .WithHouseNumber("123") .WithPoint(point) .Build();
Benefits:
- Parameters
- called so that they are more readable
- harder to mix house number with region
- can use your own order of parameters
- optional parameters may be omitted
One of the drawbacks that I can think of is that forgetting to specify one of the arguments (for example, not calling WithHouseNumber ) will result in a run-time error, and not when compiling a temporary error when using the constructor. You should also consider using additional Value objects, such as PostalCode, for example (as opposed to passing a string).
In a related note, sometimes business requirements require a change in part of the Value object. For example, when the address was originally entered, the street number may have been spelled out and should be corrected. Since you modeled the address as an immutable object, it does not have a setter. One possible solution to this problem is to introduce the "No Side Effects" function in the address value object. The function will return a copy of the object itself, with the exception of the new street name:
public class Address { private readonly String _streetName; private readonly String _houseNumber; ... public Address WithNewStreetName(String newStreetName) {
source share