public class Account { private final String userId; private final String token; public Account(String token, String userId) { this.token = token; this.userId = userId; } public String getUserId() { return userId; } public String getToken() { return token; } }
public class Account { private String userId; private String token; private Account() { // private constructor } public String getUserId() { return userId; } public String getToken() { return token; } public class Builder { private Builder() { // private constructor } public Builder setUserId(String userId) { Account.this.userId = userId; return this; } public Builder setToken(String token) { Account.this.token = token; return this; } public Account build() { return Account.this; } } }
public class Account { private String userId; private String token; private Account() { // private constructor } public String getUserId() { return userId; } public String getToken() { return token; } public static Builder newBuilder() { return new Account().new Builder(); } public class Builder { private Builder() { // private constructor } public Builder setUserId(String userId) { Account.this.userId = userId; return this; } public Builder setToken(String token) { Account.this.token = token; return this; } public Account build() { return Account.this; } } }
public class Account { private final String userId; private final String token; public Account(String userId, String token) { this.userId = userId; this.token = token; } public String getUserId() { return userId; } public String getToken() { return token; } public static class Builder { private String userId; private String token; public Builder setUserId(String userId) { this.userId = userId; return this; } public Builder setToken(String token) { this.token = token; return this; } public Account build() { return new Account(userId, token); } } }
public class Person { private final String lastName; private final String firstName; private final String middleName; private final String salutation; private final String suffix; private final String streetAddress; private final String city; private final String state; private final boolean isFemale; private final boolean isEmployed; private final boolean isHomeOwner; public Person( final String newLastName, final String newFirstName, final String newMiddleName, final String newSalutation, final String newSuffix, final String newStreetAddress, final String newCity, final String newState, final boolean newIsFemale, final boolean newIsEmployed, final boolean newIsHomeOwner) { this.lastName = newLastName; this.firstName = newFirstName; this.middleName = newMiddleName; this.salutation = newSalutation; this.suffix = newSuffix; this.streetAddress = newStreetAddress; this.city = newCity; this.state = newState; this.isFemale = newIsFemale; this.isEmployed = newIsEmployed; this.isHomeOwner = newIsHomeOwner; } public String getLastName() { return lastName; } public String getFirstName() { return firstName; } public String getMiddleName() { return middleName; } public String getSalutation() { return salutation; } public String getSuffix() { return suffix; } public String getStreetAddress() { return streetAddress; } public String getCity() { return city; } public String getState() { return state; } public boolean isFemale() { return isFemale; } public boolean isEmployed() { return isEmployed; } public boolean isHomeOwner() { return isHomeOwner; } public static class Builder { private String nestedLastName; private String nestedFirstName; private String nestedMiddleName; private String nestedSalutation; private String nestedSuffix; private String nestedStreetAddress; private String nestedCity; private String nestedState; private boolean nestedIsFemale; private boolean nestedIsEmployed; private boolean nestedIsHomeOwner; public Builder setNestedLastName(String nestedLastName) { this.nestedLastName = nestedLastName; return this; } public Builder setNestedFirstName(String nestedFirstName) { this.nestedFirstName = nestedFirstName; return this; } public Builder setNestedMiddleName(String nestedMiddleName) { this.nestedMiddleName = nestedMiddleName; return this; } public Builder setNestedSalutation(String nestedSalutation) { this.nestedSalutation = nestedSalutation; return this; } public Builder setNestedSuffix(String nestedSuffix) { this.nestedSuffix = nestedSuffix; return this; } public Builder setNestedStreetAddress(String nestedStreetAddress) { this.nestedStreetAddress = nestedStreetAddress; return this; } public Builder setNestedCity(String nestedCity) { this.nestedCity = nestedCity; return this; } public Builder setNestedState(String nestedState) { this.nestedState = nestedState; return this; } public Builder setNestedIsFemale(boolean nestedIsFemale) { this.nestedIsFemale = nestedIsFemale; return this; } public Builder setNestedIsEmployed(boolean nestedIsEmployed) { this.nestedIsEmployed = nestedIsEmployed; return this; } public Builder setNestedIsHomeOwner(boolean nestedIsHomeOwner) { this.nestedIsHomeOwner = nestedIsHomeOwner; return this; } public Person build() { return new Person( nestedLastName, nestedFirstName, nestedMiddleName, nestedSalutation, nestedSuffix, nestedStreetAddress, nestedCity, nestedState, nestedIsFemale, nestedIsEmployed, nestedIsHomeOwner); } } }
public class Person { private String lastName; private String firstName; private String middleName; private String salutation; private String suffix; private String streetAddress; private String city; private String state; private boolean isFemale; private boolean isEmployed; private boolean isHomeOwner; private Person() { // private constructor } public String getLastName() { return lastName; } public String getFirstName() { return firstName; } public String getMiddleName() { return middleName; } public String getSalutation() { return salutation; } public String getSuffix() { return suffix; } public String getStreetAddress() { return streetAddress; } public String getCity() { return city; } public String getState() { return state; } public boolean isFemale() { return isFemale; } public boolean isEmployed() { return isEmployed; } public boolean isHomeOwner() { return isHomeOwner; } public static Builder newBuilder() { return new Person().new Builder(); } public class Builder { private Builder() { // private constructor } public Builder setLastName(String lastName) { Person.this.lastName = lastName; return this; } public Builder setFirstName(String firstName) { Person.this.firstName = firstName; return this; } public Builder setMiddleName(String middleName) { Person.this.middleName = middleName; return this; } public Builder setSalutation(String salutation) { Person.this.salutation = salutation; return this; } public Builder setSuffix(String suffix) { Person.this.suffix = suffix; return this; } public Builder setStreetAddress(String streetAddress) { Person.this.streetAddress = streetAddress; return this; } public Builder setCity(String city) { Person.this.city = city; return this; } public Builder setState(String state) { Person.this.state = state; return this; } public Builder setFemale(boolean isFemale) { Person.this.isFemale = isFemale; return this; } public Builder setEmployed(boolean isEmployed) { Person.this.isEmployed = isEmployed; return this; } public Builder setHomeOwner(boolean isHomeOwner) { Person.this.isHomeOwner = isHomeOwner; return this; } public Person build() { return Person.this; } } }
public Account build() { Account account = new Account(); account.userId = Account.this.userId; account.token = Account.this.token; return account; }
Account account = Account.newBuilder() .setToken("hello") .setUserId("habr") .build();
Account.Builder accountBuilder = Account.newBuilder(); ... accountBuilder.setToken("hello"); ... accountBuilder..setUserId("habr"); return accountBuilder.build();
Source: https://habr.com/ru/post/244521/
All Articles