Answer the question
In order to leave comments, you need to log in
Beautiful Java Builder?
I read the article
. As for me, it does not reveal the essence of the panttern entirely.
The latest comment to the post
======
The buider solves a lot of tasks, I personally create it for the following purposes:
- A large number of objects are fed into the constructor, the order of which is easy to confuse
- I want to make all class fields immutable to make it a little easier for myself life in a multi-threaded environment (ideally, these are all final fields)
....
And the author's example does not solve any of these problems
======
Personally, I use something like this builder, do you think it is of high quality?
Share your builders.
We write like this:
package com.oberthur.tsm.ui.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class MyClass {
private final boolean enabled;
private final String login;
private final String password;
public static class Builder {
private boolean enabled = false;
private String login = "";
private String password = "";
public Builder myClass(MyClass myClass) {
enabled(myClass.getEnabled());
login(myClass.getLogin());
password(myClass.getPassword());
return this;
}
public static Builder myClass() {
return new Builder();
}
public Builder enabled(boolean enabled) {
this.enabled = enabled;
return this;
}
public Builder login(String login) {
this.login = login;
return this;
}
public Builder password(String password) {
this.password = password;
return this;
}
public MyClass build() {
return new MyClass(enabled, login, password);
}
}
@JsonCreator
public MyClass(
@JsonProperty("enabled") final boolean enabled,
@JsonProperty("login") final String login,
@JsonProperty("password") final String password) {
this.enabled = enabled;
this.login = login;
this.password = password;
}
public MyClass() {
enabled = false;
login = "";
password = "";
}
public Boolean getEnabled() {
return enabled;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
}
Answer the question
In order to leave comments, you need to log in
Usually builder is used where without builder you would have to write many constructors for a different set of input parameters.
An example from the Effective Java book (by Joshua Bloch):
Here is an example of a class where, due to the large number of parameters and because they are optional, you have to make a telescopic constructor
// Telescoping constructor pattern - does not scale well!
public class NutritionFacts {
private final int servingSize; // (mL) required
private final int servings; // (per container) required
private final int calories; // optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g) optional
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat, int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat, int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = carbohydrate;
}
}
// Builder Pattern
public class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder {
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int val)
{ calories = val; return this; }
public Builder fat(int val)
{ fat = val; return this; }
public Builder carbohydrate(int val)
{ carbohydrate = val; return this; }
public Builder sodium(int val)
{ sodium = val; return this; }
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question