DEV Community

Partha Sutradhar
Partha Sutradhar

Posted on

Build Step Builder Pattern to Enforce Client code to Provide data.

Introduction

The step builder pattern ensures that clients supply all essential information for constructing an object. In the DataSourceConfiguration example, clients must specify the host, port, username, and password. SSL configuration is optional.

Configuration the Datasource

The DataSourceConfiguration class is a builder pattern that allows a client to create a DataSourceConfiguration object by providing a series of steps. The steps are:

  • Step 1: Configure the host of the data source.
  • Step 2: Configure the port of the data source.
  • Step 3: Configure the username for accessing the data source.
  • Step 4: Configure the password for accessing the data source.
  • Step 5:(Optional) Configure whether to use SSL when connecting to the data source.
DataSourceConfiguration configuration = DataSourceConfiguration.Builder()
  .withHost("localhost") // required
  .withPort(3306) // required
  .withUser("root") // required
  .withPassword("password") // required
  .build(); 
Enter fullscreen mode Exit fullscreen mode

Configuration of Step Builder

public class DataSourceConfiguration {

    private final String host;
    private final int port;
    private final String username;
    private final String password;
    private final boolean useSSL;

    private DataSourceConfiguration(Builder builder) {
        this.host = builder.host;
        this.port = builder.port;
        this.username = builder.username;
        this.password = builder.password;
        this.useSSL = builder.useSSL;
    }

    public interface HostStep {
        PortStep withHost(String host);
    }

    public interface PortStep {
        UserStep withPort(int port);
    }

    public interface UserStep {
        PasswordStep withUser(String username);
    }

    public interface PasswordStep {
        BuildStep withPassword(String password);
    }

    public interface BuildStep {
        BuildStep withSSL(boolean useSSL);

        DataSourceConfiguration build();
    }

    public static class Builder implements HostStep, PortStep, UserStep, PasswordStep, BuildStep {
        private String host;
        private int port;
        private String username;
        private String password;
        private boolean useSSL;

        @Override
        public PortStep withHost(String host) {
            this.host = host;
            return this;
        }

        @Override
        public UserStep withPort(int port) {
            this.port = port;
            return this;
        }

        @Override
        public PasswordStep withUser(String username) {
            this.username = username;
            return this;
        }

        @Override
        public BuildStep withPassword(String password) {
            this.password = password;
            return this;
        }

        @Override
        public BuildStep withSSL(boolean useSSL) {
            this.useSSL = useSSL;
            return this;
        }

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

Enter fullscreen mode Exit fullscreen mode

Conclusion

The builder pattern is a valuable technique for enhancing code usability and maintainability. It guarantees that clients supply all necessary data for creating an object and improves code readability and comprehension.

Top comments (0)