[JAVA] Introduction to Design Patterns (Builder)

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.

Implementation example

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

Introduction to Design Patterns (Builder)
Introduction to design patterns (introduction)
Introduction to Design Patterns (Composite)
Introduction to design patterns (Flyweight)
Introduction to design patterns Prototype
Introduction to Design Patterns (Iterator)
Introduction to Design Patterns (Strategy)
Introduction to Design Patterns (Factory Method)
Introduction to Design Patterns (Abstract Factory)
Important design patterns to improve maintainability
Introduction to Ruby 2
Various design patterns
Design pattern ~ Builder ~
Introduction to SWING
Design pattern (2): Builder
Java Design Patterns
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Study GoF design patterns
Introduction to javac command
I read Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" (SB Creative)
Introduction to Effective java by practicing and learning (Builder pattern)
Read design patterns in Ruby
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
How to use @Builder (Lombok)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -