Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 simply using a config object.

The usage of a builder would look like this:

Product p = Product.Builder.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?

like image 826
deamon Avatar asked Aug 03 '10 08:08

deamon


People also ask

What is the difference between factory pattern and builder pattern?

A Factory Design Pattern is used when the entire object can be easily created and object is not very complex. Whereas Builder Pattern is used when the construction process of a complete object is very complex.

When should you use the builder pattern?

The builder pattern, as the name implies, is an alternative way to construct complex objects. This pattern should be used when we want to build different immutable objects using the same object building process. 3.

What is a config object?

The config object is an instantiation of javax. servlet. ServletConfig and is a direct wrapper around the ServletConfig object for the generated servlet. This object allows the JSP programmer access to the Servlet or JSP engine initialization parameters such as the paths or file locations etc.

What is the advantages of builder design pattern?

The main advantages of Builder Pattern are as follows: It provides clear separation between the construction and representation of an object. It provides better control over construction process. It supports to change the internal representation of objects.


1 Answers

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.

like image 139
Martin Hutchinson Avatar answered Oct 08 '22 15:10

Martin Hutchinson