My sample code:
class CitiesListBuilder{
private List<String> cities;
public static CitiesListBuilder newBuilder(){
return new CitiesListBuilder();
}
private CitiesListBuilder(){
cities = new ArrayList<>();
}
public CitiesListBuilder addCity(String city) {
cities.add(city);
return this;
}
public CitiesListBuilder apply(Function<List<String>, List<String>> filter) {
cities = cities.apply(filter);
return this;
}
public List<String> build(){
return cities;
}
}
To use:
CitiesListBuilder.newBuilder
.addCity("LA")
.addCity("NY")
.apply(NorthCitiesFilterFunction)
.build();
I understand my code maybe a bad sample of using builder pattern, but to me it makes the code shorter and more clear (my own opinions), and I don't see any problem with concurrent issue.
But to your opinions, what will be the arguments if I should/should not do the code likes this?
Thank you.
Aucun commentaire:
Enregistrer un commentaire