DEV Community

Partha Sutradhar
Partha Sutradhar

Posted on

3 2 2 2 2

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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay