This article summarizes the Builder. According to wikipedia, "By abstracting the process of object creation, it is possible to create dynamic objects." Reference: Builder pattern
This pattern is that in a class, if there are multiple constructors, they can be grouped together. Only one constructor with the same name and argument can be defined, and when multiple constructors are created, the number of arguments and data type will change. As the number increases, it is troublesome to make and the readability of the source code decreases. So we go out the arguments that the constructor receives.
Create a class of people who keep names, age, height, and weight as parameters
** Before applying the pattern **
public class Human {
private String name;
private Integer age;
private Integer height;
private Integer weight
public Human(String name, Integer age, Integer height, Integer weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
public Human(String name) {
this.name = name;
}
public Human(Integer age) {
this.age = age;
}
public Human(Integer height, Integer weight) {
this.height = height;
this.weight = weight;
}
}
public class Main {
public static void main(String[] args) {
Human ha = new Human("kusa");
Human hb = new Human(1);
Human hc = new Human(170, 70);
}
}
** After applying the pattern **
Builder.java
public interface Builder {
public void name(String name);
public void age(Integer age);
public void height(Integer height);
public void weight(Integer weight);
public Human getResult();
}
HumanBuilder.java
class HumanBuilder implements Builder {
private Human human;
public HumanBuilder() { this.human = new Human(); }
@Override
public void name(String name) { human.setName(name); }
@Override
public void age(Integer age) { human.setAge(age); }
@Override
public void height(Integer height) { human.setHeight(height); }
@Override
public void weight(Integer weight) { human.setWeight(weight); }
@Override
public Human getResult() { return this.human; }
}
Human.java
public class Human {
private String name;
private Integer age;
private Integer height;
private Integer weight;
public void setName(String name) { this.name = name;}
public void setAge(Integer age) { this.age = age;}
public void setHeight(Integer height) { this.height = height; }
public void setWeight(Integer weight) { this.weight = weight; }
public String getName() { return this.name; }
public Integer getAge() { return this.age; }
public Integer getHeight() { return this.height; }
public Integer getWeight() { return this.weight; }
public void hello() {
System.out.printf("name is %s %n",name);
System.out.printf("age is %d %n", age);
System.out.printf("height is %d %n", height);
System.out.printf("weight is %d %n", weight);
}
}
Director.java
class Director {
private Builder builder;
public Director(Builder builder) { this.builder = builder; }
public void construct() {
builder.name("kusakari");
builder.age(30);
builder.height(175);
builder.weight(70);
}
}
Main.java
public class Main {
public static void main(String[] args) {
Builder builder = new HumanBuilder();
Director director = new Director(builder);
director.construct();
builder.getResult().hello();
}
}
result
$ java Main
name is kusakari
age is 30
height is 175
weight is 70
As mentioned above, if there are multiple constructors in the class, the visibility of the source code will be poor and the amount of code will increase. Therefore, put the constructor on the Director class, create an instance with the HumanBuilder class that implements the Builder interface, and Main.java accesses the instance via getter.
Recommended Posts