Created
January 15, 2011 15:17
-
-
Save jeremyw/780973 to your computer and use it in GitHub Desktop.
Fluent Builder example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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