Skip to content

Instantly share code, notes, and snippets.

@Lugribossk
Created March 21, 2014 14:17
Show Gist options
  • Save Lugribossk/9687200 to your computer and use it in GitHub Desktop.
Save Lugribossk/9687200 to your computer and use it in GitHub Desktop.
Phased builders via interfaces
interface EmptyBuilder {
BuilderWithId withId(String id)
}
interface BuilderWithId {
BuilderWithTotal withTotal(BigDecimal total)
}
interface BuilderWithTotal {
CompleteBuilder withPurchaseOrderNumber(String poNumber)
}
interfce CompleteBuilder {
Invoice build()
}
public class Invoice {
private String id;
private BigDecimal total;
private String poNumber;
public static EmptyBuilder builder() {
return new Invoice.Builder();
}
private static class Builder implements EmptyBuilder, BuilderwithId, BuilderWithTotal, CompleteBuilder {
public BuilderWithId withId(String id) {
return this;
}
public BuilderWithTotal withTotal(BigDecimal total) {
return this;
}
public CompleteBuilder withPurchaseOrderNumber(String poNumber) {
return this;
}
public Invoice build() {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment