package com.example.demo.model;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Builder
public class LombokUser {
private Long id;
private String name;
private String email;
private String phone;
private String address;
@Builder.Default
private LocalDateTime createdAt = LocalDateTime.now();
@Builder.Default
private boolean active = true;
}
// Usage with Lombok:
// LombokUser user = LombokUser.builder()
// .name("John Doe")
// .email("john@example.com")
// .build();
package com.example.demo.model;
import java.time.LocalDateTime;
import java.util.Objects;
public class User {
private final Long id;
private final String name;
private final String email;
private final String phone;
private final String address;
private final LocalDateTime createdAt;
private final boolean active;
private User(Builder builder) {
this.id = builder.id;
this.name = Objects.requireNonNull(builder.name, "Name cannot be null");
this.email = Objects.requireNonNull(builder.email, "Email cannot be null");
this.phone = builder.phone;
this.address = builder.address;
this.createdAt = builder.createdAt;
this.active = builder.active;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Long id;
private String name;
private String email;
private String phone;
private String address;
private LocalDateTime createdAt = LocalDateTime.now();
private boolean active = true;
public Builder id(Long id) {
this.id = id;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Builder address(String address) {
this.address = address;
return this;
}
public Builder createdAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
return this;
}
public Builder active(boolean active) {
this.active = active;
return this;
}
public User build() {
return new User(this);
}
}
// Getters
public Long getId() { return id; }
public String getName() { return name; }
public String getEmail() { return email; }
public String getPhone() { return phone; }
public String getAddress() { return address; }
public LocalDateTime getCreatedAt() { return createdAt; }
public boolean isActive() { return active; }
}
// Usage:
// User user = User.builder()
// .name("John Doe")
// .email("john@example.com")
// .phone("555-1234")
// .build();
The Builder pattern creates complex objects step-by-step, improving readability and flexibility. I implement builders with static inner classes—fluent methods return the builder for chaining. Required fields use constructor parameters, optional fields use builder methods. The build() method validates and constructs the final object. Lombok's @Builder annotation generates builders automatically. Builders prevent telescoping constructors and make object creation self-documenting. They're ideal for objects with many parameters, especially when some are optional. Immutable objects benefit from builders since all fields are set before construction. The pattern enhances code clarity—User.builder().name("John").email("john@example.com").build() reads like natural language. Spring Boot's auto-configuration heavily uses builders internally.