İçeriğe geç
java

Java Builder Pattern İmplementasyonu

Java ile Builder Pattern implementasyonu. Immutable nesneler oluşturmak için step-by-step builder ve validation.

By Tolgahan · · 219 görüntülenme
java
public class User {
    private final Long id;
    private final String name;
    private final String email;
    private final String phone;
    private final String department;
    private final Set<String> roles;

    private User(Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.email = builder.email;
        this.phone = builder.phone;
        this.department = builder.department;
        this.roles = Collections.unmodifiableSet(builder.roles);
    }

    // Getters
    public Long getId() { return id; }
    public String getName() { return name; }
    public String getEmail() { return email; }
    public String getPhone() { return phone; }
    public String getDepartment() { return department; }
    public Set<String> getRoles() { return roles; }

    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 department;
        private Set<String> roles = new HashSet<>();

        public Builder id(Long id) {
            this.id = id;
            return this;
        }

        public Builder name(String name) {
            this.name = Objects.requireNonNull(name, "Name cannot be null");
            return this;
        }

        public Builder email(String email) {
            this.email = Objects.requireNonNull(email, "Email cannot be null");
            return this;
        }

        public Builder phone(String phone) {
            this.phone = phone;
            return this;
        }

        public Builder department(String department) {
            this.department = department;
            return this;
        }

        public Builder role(String role) {
            this.roles.add(role);
            return this;
        }

        public Builder roles(Set<String> roles) {
            this.roles.addAll(roles);
            return this;
        }

        public User build() {
            // Validation
            if (name == null || name.isBlank()) {
                throw new IllegalStateException("Name is required");
            }
            if (email == null || !email.contains("@")) {
                throw new IllegalStateException("Valid email is required");
            }
            return new User(this);
        }
    }

    @Override
    public String toString() {
        return "User{id=%d, name='%s', email='%s', roles=%s}"
            .formatted(id, name, email, roles);
    }

    // Kullanım:
    // User user = User.builder()
    //     .id(1L)
    //     .name("Tolgahan Gencer")
    //     .email("tolgahan@example.com")
    //     .department("Engineering")
    //     .role("ADMIN")
    //     .role("USER")
    //     .build();
}
Bu snippet'i göm
<iframe src="https://tolgahan.dev/snippets/java-builder-pattern-implementasyonu/embed" width="100%" height="400" frameborder="0"></iframe>
Paylaş: