Builder Object

Yeeun·2025년 4월 17일
0

SpringBoot

목록 보기
2/46

Let's break down:

BoardVO.builder().writer("테스터").build();

✅ First, this only works if your BoardVO is using Lombok with @Builder:

Example:

@Data
@Builder
public class BoardVO {
    private String writer;
    // other fields...
}

@Builder is a Lombok annotation that automatically generates a builder class for BoardVO.


🔍 Now, the breakdown:

BoardVO.builder()

  • This is a static method generated by Lombok.
  • It returns a builder object — something like BoardVO.BoardVOBuilder.
  • You can now use it to set fields in a chain-style way.

.writer("테스터")

  • This is a method generated for the writer field in your BoardVO.
  • You’re setting the value "테스터" to the writer field.

.build()

  • This final method creates the actual BoardVO object using the values you set.
  • It returns a fully built instance of BoardVO.

💡 Equivalent Plain Code:

Without using @Builder, this:

BoardVO.builder().writer("테스터").build();

Would look like this:

BoardVO board = new BoardVO();
board.setWriter("테스터");

But the builder pattern is especially helpful when:

  • Your class has many fields
  • You want immutability (no setters)
  • You want more readable object construction

"This line creates a builder object that matches the structure of the BoardVO class. Then, I can set values using the builder methods, and finally call .build() to create a BoardVO instance with those values."


🔄 What's happening in detail:

BoardVO.builder()  

👉 Creates a builder object with the same fields as BoardVO.

.writer("테스터")  

👉 Sets the writer field to "테스터" on the builder.

.build()

👉 Builds and returns a new BoardVO instance with writer set to "테스터".


So yes — the builder object mimics the structure of BoardVO, and each method like .writer() sets a field. It’s a very readable and safe way to create instances, especially when the class has many fields.

Let me know if you'd like a multi-field example too!

0개의 댓글