Search code examples
design-patternsarchitecturebuilder

Builder pattern vs Structure as parameter


If a class have too many parameters in the constructor we can either use a structure or a builder pattern.

Which is better? Is there any good practice?


Solution

  • Use a class with structure that meets your need. Martin Fowler has one similar technique called DataTransferObject (DTO), in which you pass an object model instead of retrieving the data using multiple calls (persumably database calls), reducing the cost.

    The benefit over builder pattern:

    1. The class structure has no additional logic, means it is easier to read (less logic)
    2. Do not create constructor doing real works code smell, again makes the code easier to read
    3. The operation is cheap, since you just passing the references and not creating a brand new object. Especially when the data structure is big.

    The downside: The object is in mutable state.