[Java] Implement a function that uses a class implemented in the Builder pattern

Annotation

A Builder pattern is used for all implementations that return itself after setting something.

What I wanted to do

The following code was mass-produced for inserting using SimpleJdbcInsert.

new SimpleJdbcInsert(Jdbc template)
        .Builder processing
        .execute(new BeanPropertySqlParameterSource(Object to insert));

In this code, it is inefficient to new`` SimpleJdbcInsert and BeanPropertySqlParameterSource one by one, and there was a demand for efficiency in the entire project for other reasons, so efficiency is improved by wrapping the process with a function. I tried to figure it out.

What became a problem

Since the SimpleJdbcInsert is implemented in the Builder pattern, I could only think of an implementation by passing an initialized instance that implements the function. However, this is almost not efficient at all.

Code that is not efficient at all


Wrap function(new SimpleJdbcInsert(Jdbc template).Builder processing,Object to insert);

Solution

I was able to implement it efficiently by writing it like a Fluent Builder pattern (loan pattern).

//Wrap function
public int insertExecute(UnaryOperator<SimpleJdbcInsert> builder, Object entity) {
    return builder.apply(new SimpleJdbcInsert(Jdbc template))
            .execute(new BeanPropertySqlParameterSource(entity));
}

//Usage example
insertExecute(it -> it.Builder processing,Object to insert);

It's a lack of study that the idea that "if the generation process by the builder is an obstacle, you should accept it" did not come up at once.

Articles that I used as a reference

Builder pattern written in Java -Qiita

Recommended Posts

[Java] Implement a function that uses a class implemented in the Builder pattern
How to implement a job that uses Java API in JobScheduler
A bat file that uses Java in windows
Write a class that can be ordered in Java
Implement the same function as C, C ++ system ("cls"); in Java
3 Implement a simple interpreter in Java
Implement PHP implode function in Java
Sample code that uses the Mustache template engine JMustache in Java
What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Implement something like a stack in Java
Sample program that returns the hash value of a file in Java
Are you still exhausted to implement the search function? Gem'ransack' that can be implemented in an instant
A story about the JDK in the Java 11 era
Measure the size of a folder in Java
A story about making a Builder that inherits the Builder
[Java] Where is the implementation class of annotation that exists in Bean Validation?
A quick review of Java learned in class
General-purpose StringConverter class that utilizes generics in Java8
Get multiple Resources that match the pattern in spring
Write a class in Kotlin and call it in Java
A note for Initializing Fields in the Java tutorial
I tried to make a login function in Java
Quickly implement a singleton with an enum in Java
A quick review of Java learned in class part3
Builder pattern that forces a set of required properties
Develop a Java application that uses Amazon SQS (Part 1)
I tried to implement the Euclidean algorithm in Java
Summarize the additional elements of the Optional class in Java 9
The story that .java is also built in Unity 2018
A library that realizes multi-line strings in Java multiline-string
A program (Java) that outputs the sum of odd and even numbers in an array
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
How to get the class name / method name running in Java
Implement Thread in Java and try using anonymous class, lambda
A quick explanation of the five types of static in Java
Let's make a calculator application in Java ~ Display the application window
Determine that the value is a multiple of 〇 in Ruby
Implement Java Interface in JRuby class and call it from Java
Identify threads in the Java process that are wasting CPU
Learning record that created the tweet function with the STI pattern
A program that counts the number of words in a List
How to use the function implemented in Kotlin Interface introduced in Maven as the default implementation from Java 8
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 2]
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 1]
Implement application function in Rails
Implement two-step verification in Java
Implement Basic authentication in Java
Find a subset in Java
Implement math combinations in Java
Enum Strategy pattern in Java
2 Implement simple parsing in Java
[Java] Draw a simple pattern
Implement Email Sending in Java
[Java] I implemented the combination.
Implement functional quicksort in Java
Implement rm -rf in Java.
Implement XML signature in Java
[Java] Get date information 10 days later using milliseconds in the Date class
[Rails] A simple way to implement a self-introduction function in your profile