[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
[Java] Implement a function that uses a class implemented in the Builder pattern
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
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
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 /?)
Implement something like a stack in Java
Creating a matrix class in Java Part 1
Are you still exhausted to implement the search function? Gem'ransack' that can be implemented in an instant
GetInstance () from a @Singleton class in Groovy from Java
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
A quick review of Java learned in class part4
Write a class in Kotlin and call it in Java
A note for Initializing Fields in the Java tutorial
[Ruby / Rails] Set a unique (unique) value in the class
I tried to make a login function in Java
Quickly implement a singleton with an enum in Java
Builder pattern that forces a set of required properties
Develop a Java application that uses Amazon SQS (Part 1)
A quick review of Java learned in class part2
Develop a Java application that uses Amazon SQS (Part 2)
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
Builder pattern (Effective Java)
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
Implement Thread in Java and try using anonymous class, lambda
A quick explanation of the five types of static in Java
Implement Java Interface in JRuby class and call it from Java
Identify threads in the Java process that are wasting CPU
Create a method to return the tax rate in Java
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 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