Created
March 21, 2014 14:17
-
-
Save Lugribossk/9687200 to your computer and use it in GitHub Desktop.
Phased builders via interfaces
This file contains 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
interface EmptyBuilder { | |
BuilderWithId withId(String id) | |
} |
This file contains 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
interface BuilderWithId { | |
BuilderWithTotal withTotal(BigDecimal total) | |
} |
This file contains 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
interface BuilderWithTotal { | |
CompleteBuilder withPurchaseOrderNumber(String poNumber) | |
} |
This file contains 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
interfce CompleteBuilder { | |
Invoice build() | |
} |
This file contains 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 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