Factory Pattern

Implement a factory that will return the object on static method. In contrast to Abstract Factory Pattern, the method is implemented in its own class.

However classes that extends Template here will inherit the factory as well.

package designpatterns.factory;

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

  public final boolean isPublic;

  public Template(String color, String templateName) {
    this.color = color;
    this.templateName = templateName;
    isPublic = false;
  }


  public static Template createRedTemplate(String templateName) {
    return new Template("red", templateName);
  }

  public static Template createBlueTemplate(String templateName) {
    return new Template("blue", templateName);
  }

  public static Template createGreenTemplate(String templateName) {
    return new Template("green", templateName);
  }
}

package designpatterns.factory.test;

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

public class TemplateTest {
  @Test
  void endToEndTest_givenTemplateFactory_shouldReturnTemplate() {
    Template redTemplate = Template.createBlueTemplate("myTemplate");
    Template blueTemplate = Template.createBlueTemplate("myTemplate");
    Template greenTemplate = Template.createGreenTemplate("myTemplate");

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