java - Builder pattern vs. config object -


Builder patterns are popular for creating irreversible objects, but there are some programming overheads to make a builder so I wonder Why not just use a config object?

The use of a builder will appear as:

  product page = product. Name ("vodka"). Alcohol (0.38) .size (0.7) .price (17.99) .build (); It is clear that it is very readable and concise, but you have to implement the builder:  
  public class product {the name of the public final string; Public last float alcohol; Public last float size; Public last float price; Personal Product (Builder Builder) {this.name = builder.name; this. Alcohol = Builder alcohol; This.size = builder.size; this. Value = builder value; } Public Static Class Builder {Name of Private String; 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; } Size of the public builder (float size) {this.size = size; Return.this; } Public Builder Price (Float Price) {this.price = price; Return.this; } Public product creation () {Return new product (this); }}}  

I think, to reduce the code by using a simple config object like this:

  class ProductConfig {public string name of the; Public float alcohol; Public float size; Price of public float; // name still mandatory public productConfig (string name) {this.name = name; }} Public class product {the name of the public final string; Public last float alcohol; Public last float size; Public last 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 some more lines but it is very readable, but the implementation is very easy and it is probably easy to understand for the person who is not familiar with the builder pattern. By the way: is there a name for this method?

What is ignored in the config application that I have ignored?

post builder pattern decoupling improvements - if your product is an interface and builder only the orbit that can be aware of implementation (or implementation, in some cases) The builder also applies an interface so you can inject it into your code so that you can increase the decoupling further.

This decoupling means that your code is more compact and easy to test


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -