deamon deamon - 6 months ago 39
Java Question

Builder pattern vs. config object

The builder pattern is popular to create immutable objects, but there is some programming overhead to create a builder. So I wonder why not to use simply a config object.

The usage of a builder would look like this:

Product p = Product.name("Vodka").alcohol(0.38).size(0.7).price(17.99).build();


It is obvious that this is very readable and concise, but you have to implement the builder:

public class Product {

public final String name;
public final float alcohol;
public final float size;
public final float price;

private Product(Builder builder) {
this.name = builder.name;
this.alcohol = builder.alcohol;
this.size = builder.size;
this.price = builder.price;
}

public static class Builder {

private String name;
private float alcohol;
private float size;
private float price;

// mandatory
public static Builder name(String name) {
Builder b = new Builder();
b.name = name;
return b;
}

public Builder alcohol(float alcohol) {
this.alcohol = alcohol;
return.this;
}

public Builder size(float size) {
this.size = size;
return.this;
}

public Builder price(float price) {
this.price = price;
return.this;
}

public Product build() {
return new Product(this);
}

}

}


My idea is, to reduce the code by using a simple config object like this:

class ProductConfig {

public String name;
public float alcohol;
public float size;
public float price;

// name is still mandatory
public ProductConfig(String name) {
this.name = name;
}

}

public class Product {

public final String name;
public final float alcohol;
public final float size;
public final float price;

public Product(ProductConfig config) {
this.name = config.name;
this.alcohol = config.alcohol;
this.size = config.size;
this.price = config.price;
}

}


Usage:

ProductConfig config = new ProductConfig("Vodka");
config.alcohol = 0.38;
config.size = 0.7;
config.price = 17.99;
Product p = new Product(config);


This usage needs a few more lines but is also very readable, but the implementation is much simpler and maybe it is easier to understand for someone who isn't familiar with the builder pattern. By the way: is there a name for this pattern?

Is there a drawback in the config approach that I've overlooked?

Answer Source

The builder pattern improves decoupling - your Product can be an interface and the only class that knows about the implementation (or implementations, in some cases) is the builder. If the builder also implements an interface then you can inject this into your code to increase decoupling further.

This decoupling means your code is more maintainable and easier to test.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download