--Java 8 ou version ultérieure
Lorsque vous souhaitez créer un objet avec une certaine propriété Cette fois, considérons un objet appelé Person comme exemple.
Le code Builder est ci-dessous.
Person
package com.example;
import java.util.Optional;
public class Person {
private final String name;// Required
private final Integer age;// Required
private final String gender;// Required
private final Integer height;// Required
private final Optional<String> eyeColor;// Optional(use Optional<String>)
private final Optional<String> hairColor;// Optional(use Optional<String>)
private final Optional<String> hobby;// Optional(use Optional<String>)
Person(Builder.Builder1 builder) {
this.name = builder.name;
this.age = builder.age;
this.gender = builder.gender;
this.height = builder.height;
this.eyeColor = builder.eyeColor;
this.hairColor = builder.hairColor;
this.hobby = builder.hobby;
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
public Builder1 name(String name) {
return new Builder1(name);
}
public static final class Builder1 {
final String name;
Integer age;
String gender;
Integer height;
Optional<String> eyeColor;
Optional<String> hairColor;
Optional<String> hobby;
private Builder1(String name) {
this.name = name;
}
public Builder2 age(Integer age) {
this.age = age;
return new Builder2(Builder1.this);
}
}
public static final class Builder2 {
final Builder1 builder;
private Builder2(Builder1 builder) {
this.builder = builder;
}
public Builder3 gender(String gender) {
this.builder.gender = gender;
return new Builder3(this.builder);
}
}
public static final class Builder3 {
final Builder1 builder;
private Builder3(Builder1 builder) {
this.builder = builder;
}
public Builder4 height(Integer height) {
this.builder.height = height;
return new Builder4(this.builder);
}
}
public static final class Builder4 {
final Builder1 builder;
private Builder4(Builder1 builder) {
this.builder = builder;
}
public Builder4 eyeColor(String eyeColor){
this.builder.eyeColor = Optional.of(eyeColor);
return this;
}
public Builder4 hairColor(String hairColor){
this.builder.hairColor = Optional.of(hairColor);
return this;
}
public Builder4 hobby(String hobby){
this.builder.hobby = Optional.of(hobby);
return this;
}
public Person build() {
return new Person(this.builder);
}
}
}
public String name() {
return this.name;
}
public Integer age() {
return this.age;
}
public String gender() {
return this.gender;
}
public Integer height() {
return this.height;
}
public Optional<String> eyeColor() {
return this.eyeColor;
}
public Optional<String> hairColor() {
return this.hairColor;
}
public Optional<String> hobby() {
return this.hobby;
}
@Override
public String toString() {
return "Person(name=" + this.name + ", age=" + this.age + ", gender=" + this.gender + ", height=" + this.height + ", eyeColor=" + this.eyeColor + ", hairColor=" + this.hairColor + ", hobby=" + this.hobby + ")";
}
public void doSomething() {
// do something
}
}
--Assignez un générateur distinct pour chaque méthode de réglage pour les propriétés requises --Retourne le générateur de la méthode suivante comme valeur de retour du setter
package test;
import com.example.Person;
public class Main {
public static void main(String[] args) {
Person person = Person.builder().name("Tom").age(20).gender("male").height(200).build();
System.out.println(person);
}
}
TelescopingConstructor
public Person(String name, Integer age, String gender, Integer height, String eyeColor, String hairColor, String hobby) {
this.name = name;
this.age = age;
this.gender = gender;
this.height = height;
this.eyeColor = eyeColor;
this.hairColor = hairColor;
this.hobby = hobby;
}
(*) Nous avons créé un outil de génération automatique pour atténuer les inconvénients. https://riversun.github.io/java-builder/
Recommended Posts