Here are a few examples of "Builder", if you don't understand, that's your problem. Practice more again.
public class Person {
private final int id;
private final String name;
private final int age;
public static class Builder {
private String name;
private int id = 0;
private int age = 0;
public Builder(String name) {
this.name = name;
}
public Builder age(int age) {
age = age;
return this;
}
public Builder id(int id) {
id = id;
return this;
}
public Person build() {
return new Person(this);
}
}
public Person(Builder builder) {
this.id = builder.id;
this.name = builder.name;
this.age = builder.age;
}
public static void main(String[] args) {
Person person = new Builder("test")
.age(23).id(2).build();
}
}
public abstract class Pizza {
public enum Topping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE}
final Set<Topping> toppings;
abstract static class Builder<T extends Builder<T>> {
EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);
public T addTopping(Topping topping) {
toppings.add(Objects.requireNonNull(topping));
return self();
}
protected abstract T self();
abstract Pizza build();
}
Pizza(Builder<?> builder) {
toppings = builder.toppings.clone();
}
}
public class NyPizza extends Pizza {
public enum Size {SMALL, MEDIUM, LAGER}
private final NyPizza.Size size;
public static class Builder extends Pizza.Builder<NyPizza.Builder> {
private final Size size;
public Builder(NyPizza.Size size) {
this.size = Objects.requireNonNull(size);
}
@Override
public NyPizza build() {
return new NyPizza(this);
}
protected Builder self() {
return this;
}
}
private NyPizza(Builder builder) {
super(builder);
size = builder.size;
}
}
I think you'd better learn the second example, although the first example is enough to express this usage clearly.
Finally, I hope you can see the equal sign alignment in the first example and use this style.