Builder Pattern

Create a builder so that it can build the class when having too much parameters in the constructor

package designpatterns.builder;

public class Template {
  public final String color;
  public final String templateName;
  public final boolean isPublic;


  public Template(Builder templateBuilder) {
    this.color = templateBuilder.color;
    this.templateName = templateBuilder.templateName;
    this.isPublic = templateBuilder.isPublic;
  }

  public static Builder builder() {
    return new Builder();
  }

  public static class Builder {
    private String color;
    private String templateName;
    private boolean isPublic;

    public Builder() {

    }

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

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

    public Builder setIsPublic(boolean isPublic) {
      this.isPublic = isPublic;
      return this;
    }

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

package designpatterns.builder.test;

import designpatterns.builder.Template;
import org.junit.jupiter.api.Test;

public class TemplateBuilderTest {
  @Test
  void endToEnd_givenTemplateBuilder_shouldBuildTemplate() {
    Template redTemplate = Template.builder().fromTemplateName("my template").fromColor("red").setIsPublic(false).build();
    Template blueTemplate = Template.builder().fromTemplateName("my template").fromColor("blue").setIsPublic(false).build();
    Template greenTemplate = Template.builder().fromTemplateName("my template").fromColor("green").setIsPublic(false).build();

    System.out.println(redTemplate.color);
    System.out.println(blueTemplate.color);
    System.out.println(greenTemplate.color);
  }
}