April 15, 2023
Developers write Java source code (.java files).
The Java compiler reads the Java source code (.java files) and compiles them into bytecode (.class files). Bytecode (.class files) are readable by the JVM (Java Virtual Machine) but not directly by computers. (java -> class)
The compiled bytecode (.class files) is passed to the JVM’s Class Loader.
The Class Loader loads necessary classes into the runtime data area (JVM memory) through dynamic loading. The detailed operations of the Class Loader include:
The Execution Engine within the JVM fetches bytecode instructions from the JVM memory and executes them. There are two execution modes:
In essence, the Builder Pattern is a technique to make object creation clean and flexible.
It emerged as an alternative to the telescoping constructor pattern and JavaBeans pattern. Let’s first look at why the Builder Pattern came into existence.
public class Post {
private String title;
private String content;
private LocalDateTime date;
// Telescoping constructor pattern
public Post() {
}
public Post(String title) {
this.title = title;
}
public Post(String title, String author) {
this.title = title;
this.author = author;
}
public Post(String title, String author, LocalDateTime date) {
this.title = title;
this.author = author;
this.date = date;
}
}
Disadvantages
public class Post {
private String title;
private String content;
private LocalDateTime date;
// JavaBeans pattern
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public LocalDateTime getDate() {
return this.publishedAt;
}
public void setDate(LocalDateTime publishedAt) {
this.publishedAt = publishedAt;
}
}
@SpringBootTest
public class BuilderPatternTest {
@Test
public void Test() {
Post post = new Post();
post.setTitle("홍길동전");
post.setAuthor("허균");
post.setPublishedAt(LocalDateTime.now());
post.setPageCount(0);
}
}
Disadvantages
The Builder pattern emerged as an alternative to these patterns.
public Book(BookBuilder bookBuilder) {
title = bookBuilder.title;
author = bookBuilder.author;
publishedAt = bookBuilder.publishedAt;
pageCount = bookBuilder.pageCount;
}
public static class BookBuilder {
// Required parameters
private String title;
// Optional parameters
private String author = "";
private LocalDateTime publishedAt = LocalDateTime.now();
private int pageCount = 0;
public BookBuilder (String title) {
this.title = title;
}
public BookBuilder author(String value) {
author = value;
return this; // Return 'this' to chain method calls with dot notation.
}
public BookBuilder publishedAt(LocalDateTime value) {
publishedAt = value;
return this;
}
public BookBuilder pageCount(int value) {
pageCount = value;
return this;
}
public Book build() {
return new Book(this);
}
}
@SpringBootTest
public class BuilderPatternTest {
@Test
public void Test() {
Book book = new Book.BookBuilder("홍길동전")
.author("허균")
.build();
}
}
Using the Builder pattern is straightforward and provides better readability and flexibility in object creation.
Moreover, lombok’s @Builder
annotation simplifies implementation:
@Builder
public class User {
private String title;
private String author = "";
private LocalDateTime date = LocalDateTime.now();
private int pageCount = 0;
}
User user = User.builder()
.title("title")
.author("author")
.build();