vendredi 21 février 2020

Builder pattern (java). Why is Builder class public?

Why every where I look the inner builder class is public static? Shouldn't it be private static? Maybe I don't see something but I would say it would be better. Could anyone explain it to me?

@Getter @AllArgsConstructor @ToString
public class CarDto {

private final String make;
private final int seatCount;
private final String type;
private final CarDto carDto;

public static Builder builder(){
    return new Builder();
}


private static class Builder{
    private String make;
    private int seatCount;
    private String type;
    private CarDto carDto;


    public Builder make(String make){
        this.make = make;
        return this;
    }

    public Builder seatCount(int seatCount){
        this.seatCount = seatCount;
        return this;
    }

    public Builder type(String type){
        this.type = type;
        return this;
    }

    public Builder carDto(CarDto carDto){
        this.carDto = carDto;
        return this;
    }

    public CarDto create(){
        return new CarDto(make, seatCount,type,carDto);
    }
}
}

Aucun commentaire:

Enregistrer un commentaire