51. Writing an immutable class via the Builder pattern

When a class (immutable or mutable) has too many fields, it requires a constructor with many arguments. When some of those fields are required and others are optional, this class will need several constructors to cover all the possible combinations. This becomes cumbersome for the developer and for the user of the class. This is where the Builder pattern comes to the rescue.

According to the Gang of Four (GoF)—the Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.

The Builder pattern can be implemented as a separate class or as an inner static class. Let's focus on the second case. The User class has three required fields (nickname, password, and created) and three optional fields (email, firstname, and lastname).

Now, an immutable User class relying on the Builder pattern will appear as follows:

public final class User {

private final String nickname;
private final String password;
private final String firstname;
private final String lastname;
private final String email;
private final Date created;

private User(UserBuilder builder) {
this.nickname = builder.nickname;
this.password = builder.password;
this.created = builder.created;
this.firstname = builder.firstname;
this.lastname = builder.lastname;
this.email = builder.email;
}

public static UserBuilder getBuilder(
String nickname, String password) {
return new User.UserBuilder(nickname, password);
}

public static final class UserBuilder {

private final String nickname;
private final String password;
private final Date created;
private String email;
private String firstname;
private String lastname;

public UserBuilder(String nickname, String password) {
this.nickname = nickname;
this.password = password;
this.created = new Date();
}

public UserBuilder firstName(String firstname) {
this.firstname = firstname;
return this;
}

public UserBuilder lastName(String lastname) {
this.lastname = lastname;
return this;
}

public UserBuilder email(String email) {
this.email = email;
return this;
}

public User build() {
return new User(this);
}
}

public String getNickname() {
return nickname;
}

public String getPassword() {
return password;
}

public String getFirstname() {
return firstname;
}

public String getLastname() {
return lastname;
}

public String getEmail() {
return email;
}

public Date getCreated() {
return new Date(created.getTime());
}
}

Here are some usage examples:

import static modern.challenge.User.getBuilder;
...
// user with nickname and password
User user1 = getBuilder("marin21", "hjju9887h").build();

// user with nickname, password and email
User user2 = getBuilder("ionk", "44fef22")
.email("[email protected]")
.build();

// user with nickname, password, email, firstname and lastname
User user3 = getBuilder("monika", "klooi0988")
.email("[email protected]")
.firstName("Monika")
.lastName("Ghuenter")
.build();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.60.63