Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomerBuilder {
- private String surname;
- private String firstName;
- private String ssn;
- public static CustomerBuilder customer() {
- return new CustomerBuilder();
- }
- public CustomerBuilder withSurname(String surname) {
- this.surname = surname;
- return this;
- }
- public CustomerBuilder withLastName(String lastName) {
- this.lastName = lastName;
- return this;
- }
- public CustomerBuilder withSSN(String ssn) {
- this.ssn = ssn;
- return this;
- }
- // client doesn't get to instantiate Customer directly
- public Customer build() {
- if (surname == null || firstName == null || ssn == null) {
- throw new NullPointerException("Incomplete customer definition");
- }
- /* option 1: would have package private setters */
- Customer target = new Customer();
- target.setSurname(this.surname);
- target.setFirstName(this.firstName);
- target.setSSN(this.ssn);
- return target;
- /* option 2:
- I actually like the approach marcio suggests, where we pass the
- builder in the constructor - prevents the need for all of the
- setters
- Customer target = new Customer(this);
- return target;
- */
- }
- }
- import static com.acme.CustomerBuilder.customer;
- public class Client {
- public void doSomething() {
- Customer customer = customer()
- .withSurname("Smith")
- .withFirstName("Fred")
- .withSSN("123XS1")
- .build();
- }
- }
- public class NutritionFacts {
- private final int servingSize;
- private final int servings;
- private final int calories;
- private final int fat;
- private final int sodium;
- private final int carbohydrate;
- public static class Builder {
- // required parameters
- private final int servingSize;
- private final int servings;
- // optional parameters
- private int calories = 0;
- private int fat = 0;
- private int carbohydrate = 0;
- private int sodium = 0;
- public Builder(int servingSize, int servings) {
- this.servingSize = servingSize;
- this.servings = servings;
- }
- public Builder calories(int val)
- { calories = val; return this; }
- public Builder fat(int val)
- { fat = val; return this; }
- public Builder carbohydrate(int val)
- { carbohydrate = val; return this; }
- public Builder sodium(int val)
- { sodium = val; return this; }
- public NutritionFacts build() {
- return new NutritionFacts(this);
- }
- }
- private NutritionFacts(Builder builder) {
- servingSize = builder.servingSize;
- servings = builder.servings;
- calories = builder.calories;
- fat = builder.fat;
- soduim = builder.sodium;
- carbohydrate = builder.carbohydrate;
- }
- }
- NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
- calories(100).sodium(35).carbohydrate(27).build();
- setOuterBounds(x, y, width, height);
- setInnerBounds(x + 2, y + 2, width - 4, height - 4);
- setOuterBounds(bounds);
- setInnerBounds(bounds.expand(-2));
- public static void setEmail(String newEmail){
- this.email = newEmail;
- }
- public static String getEmail(){
- return this.email;
- }
- ContactInfo cinfos = new ContactInfo[] {
- new ContactInfo("home", "+123456789", "123 ABC Avenue"),
- new ContactInfo("biz", "+987654321", "789 ZYX Avenue")
- };
- Customer c = new Customer("john", "doe", cinfos);
Add Comment
Please, Sign In to add comment