Note that there are some explanatory texts on larger screens.

plurals
  1. POThe builder pattern and a large number of mandatory parameters
    primarykey
    data
    text
    <p>To date I use the <a href="http://rwhansen.blogspot.com/2007/07/theres-builder-pattern-that-joshua.html">following</a> implementation of the builder pattern (as opposed to the implementation described <a href="http://en.wikipedia.org/wiki/Builder_pattern#Java">here</a>):</p> <pre><code>public class Widget { public static class Builder { public Builder(String name, double price) { ... } public Widget build() { ... } public Builder manufacturer(String value) { ... } public Builder serialNumber(String value) { ... } public Builder model(String value) { ... } } private Widget(Builder builder) { ... } } </code></pre> <p>This works well for most situations I've encountered where I need to build up a complex object with a variety of required/mandatory and optional parameters. However, I've been struggling lately to understand how the pattern is of any benefit when all your parameters are mandatory (or at least the vast majority are).</p> <p>One means of getting around this has been to logically group the parameters being passed in to their own classes to reduce the number of parameters being passed to the builder constructor. </p> <p>For example, instead of:</p> <pre><code>Widget example = new Widget.Builder(req1, req2, req3,req4,req5,req6,req7,req8) .addOptional(opt9) .build(); </code></pre> <p>becomes grouped as follows:</p> <pre><code>Object1 group1 = new Object1(req1, req2, req3, req4); Object2 group2 = new Object2(req5, req6); Widget example2 = new Widget.Builder(group1, group2, req7, req8) .addOptional(opt9) .build(); </code></pre> <p>While having separate objects simplifies things quite a bit, it also makes things a little difficult to follow if one is not familiar with the code. One thing I considered was moving all parameters into their own <code>addParam(param)</code> methods and then performing validation on required parameters in the <code>build()</code> method.</p> <p>What is best practice and is there perhaps a better approach to this that I haven't considered?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload