DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on • Edited on

Builder Pattern

This pattern is particularly useful when you have an object with many optional parameters, and you want to make it easy to construct instances of that object.

Step 1: Define the User class

First, let's create the User class that we want to build using the Builder pattern:

public class User {
    private final String username; // Required
    private final String email;    // Required
    private int age;               // Optional
    private String address;        // Optional

    // Private constructor to enforce the use of the Builder
    private User(String username, String email, int age, String address) {
        this.username = username;
        this.email = email;
        this.age = age;
        this.address = address;
    }

    public void display() {
        System.out.println("User Details:");
        System.out.println("Username: " + username);
        System.out.println("Email: " + email);
        System.out.println("Age: " + age);
        System.out.println("Address: " + address);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the UserBuilder class

Next, we create a UserBuilder class that will help us build User objects step by step:

public class UserBuilder {
    private final String username; // Required
    private final String email;    // Required
    private int age;               // Optional
    private String address;        // Optional

    // Constructor with required fields
    public UserBuilder(String username, String email) {
        this.username = username;
        this.email = email;
    }

    // Setter methods for optional fields
    public UserBuilder age(int age) {
        this.age = age;
        return this;
    }

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

    // Build method to create the User object
    public User build() {
        return new User(username, email, age, address);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Using the Builder

Now, let's demonstrate how to create a User object using the UserBuilder:

public class UserManagementApp {
    public static void main(String[] args) {
        User user1 = new UserBuilder("john_doe", "john@example.com")
            .age(30)
            .address("123 Main St")
            .build();

        User user2 = new UserBuilder("jane_smith", "jane@example.com")
            .age(25)
            .build();

        user1.display();
        System.out.println();
        user2.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

User Details:
Username: john_doe
Email: john@example.com
Age: 30
Address: 123 Main St

User Details:
Username: jane_smith
Email: jane@example.com
Age: 25
Address: null
Enter fullscreen mode Exit fullscreen mode

In this example:

  • The User class represents a user with some required fields (username and email) and optional fields (age and address).
  • The UserBuilder class provides methods to set the optional fields and a build() method to create a User object with the specified parameters.
  • We use the UserBuilder to create two User objects, one with all optional fields and another with only some of them.

The Builder Design Pattern makes it easier to construct objects with many optional parameters while providing a clear and readable way to set those parameters. It's particularly useful when you want to create objects with different configurations without having multiple overloaded constructors or setters.

😍 If you enjoy the content, please 👍 like, 🔄 share, and 👣 follow for more updates!

Top comments (0)