Java Record Builder Pattern Example

1. Introduction

In this blog post, we explore the integration of the Builder Pattern with Java Records. The Builder Pattern is widely used in Java for creating complex objects with multiple optional parameters in a readable and maintainable way. With the introduction of records in Java, there's a unique opportunity to combine these concepts for efficient and clean code.

Definition

Java Records, introduced in Java 16, provide a concise way to create immutable data classes. When combined with the Builder Pattern, records offer a powerful approach to handle complex object creation with many optional fields, enhancing code readability and maintainability.

2. Example Steps

1. Define a Java Record with multiple fields.

2. Implement a nested Builder class within the record.

3. Add methods in the Builder for setting fields and validation.

4. Use the Builder to create instances of the record.

3. Code Program

// Complex Record with Builder Pattern
public record Employee(String name, String department, String email, int age) {
    // Nested Builder class
    public static class Builder {
        private String name;
        private String department;
        private String email;
        private int age;

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

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

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

        public Builder age(int age) {
            if (age < 0) throw new IllegalArgumentException("Age cannot be negative");
            this.age = age;
            return this;
        }

        public Employee build() {
            return new Employee(name, department, email, age);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an Employee using the Builder
        Employee employee = new Employee.Builder()
                                .name("Alice")
                                .department("Engineering")
                                .email("alice@example.com")
                                .age(30)
                                .build();

        System.out.println(employee);
    }
}

Output:

Employee[name=Alice, department=Engineering, email=alice@example.com, age=30]

Explanation:

1. The Employee record is defined with fields name, department, email, and age.

2. A nested Builder class within the Employee record provides a fluent API to set these fields.

3. The Builder includes validation in the age method to ensure the age is not negative.

4. An Employee instance is created using the Builder, with chained method calls for setting fields.

5. The program outputs the details of the created Employee object, demonstrating the integration of the Builder Pattern with Java records for constructing complex objects.


Comments