Skip to content

Instantly share code, notes, and snippets.

@jeremyw
Created January 15, 2011 15:17
Show Gist options
  • Save jeremyw/780973 to your computer and use it in GitHub Desktop.
Save jeremyw/780973 to your computer and use it in GitHub Desktop.
Fluent Builder example
public class Car {
public static class Builder {
private int year;
private String make;
private String model;
private String color;
public Builder year(int year) {
this.year = year; return this;
}
public Builder make(String make) {
this.make = make; return this;
}
public Builder model(String model) {
this.model = model; return this;
}
public Builder color(String color) {
this.color = color; return this;
}
public Car build() {
return new Car(year, make, model, color);
}
}
private final int year;
private final String make;
private final String model;
private final String color;
private Car(int year, String make, String model, String color) {
this.year = year;
this.make = make;
this.model = model;
this.color = color;
}
public static Builder builder() {
return new Builder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment