When common processing is required between multiple instances, by hiding that processing An implementation that can make the internal processing different even for the same processing. Here, Japanese and Americans are created based on the People class, which has a field called mainLanguage.
Create an interface that defines common processing between multiple instances. (= Builder class)
Builder.java
public interface Builder {
void createMainLanguage(); //Set the main language
People getResult(); //Return People instance
}
People.java
public class People {
//subject
private String mainLanguage;
public String getMainLanguage() {
return mainLanguage;
}
public void setMainLanguage(String mainLanguage) {
this.mainLanguage = mainLanguage;
}
void hello() {
System.out.println("Hello. The main language is" + mainLanguage);
}
}
Create a class with an implementation of the defined interface. (= ConcreteBuilder class) ** When the types of instances to be created increase, generate and increase this class **
JapaneseBuilder.java
//Japanese Builder
public class JapaneseBuilder implements Builder {
private People people;
public JapaneseBuilder() {
this.people = new People();
}
@Override
public void createMainLanguage() {
people.setMainLanguage("Japanese"); //Japanese is the main language
}
@Override
public People getResult() {
return this.people;
}
}
AmericanBuilder.java
//American Builder
public class AmericanBuilder implements Builder {
private People people;
public AmericanBuilder() {
this.people = new People();
}
@Override
public void createMainLanguage() {
people.setMainLanguage("English"); //English is the main language
}
@Override
public People getResult() {
return this.people;
}
}
Create a class that is responsible for the actual instantiation. (= Director class) ** * Make sure to have the interface as an argument of the constructor ** By passing Builder to Director, it becomes independent of ConcreteBuilder class. In other words, Director is unaware of what the actual class, ConcreteBuilder, is.
Director.java
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public People construct() {
builder.createMainLanguage();
return builder.getResult();
}
}
(1) Create a builder instance that has a builder interface as a type. The actual processing depends on ConcreteBuilder at the time of generation. (2) Create a Director instance with the builder instance as an argument.
Main.java
public class Main {
public static void main(String[] args) {
if (args[0].equals("Japanese")) {
//Generate Japanese builder
Builder builder = new JapaneseBuilder();
//The Director is just passing the Bulder interface, so I don't even know the internal implementation of the Builder.
Director director = new Director(builder);
People people = director.construct();
people.hello();
} else if (args[0].equals("English")) {
Builder builder = new AmericanBuilder();
Director director = new Director(builder);
People people = director.construct();
people.hello();
} else {
throw new IllegalArgumentException();
}
}
}
Recommended Posts