the difference between:
board.setWriter("테스터");
and
BoardVO.builder().writer("테스터").build();
Let’s break it down gently and clearly:
setWriter("테스터") – Traditional WayThis is the normal setter method used when:
newExample:
BoardVO board = new BoardVO();
board.setWriter("테스터");
👉 This changes the object after it's created.
.builder().writer("테스터").build(); – Builder PatternThis is the builder pattern, used when:
Example:
BoardVO board = BoardVO.builder().writer("테스터").build();
👉 This sets the values during object construction, and the object is ready to use as soon as it’s built.
| Feature | setWriter() | builder().writer().build() |
|---|---|---|
| Object creation | new BoardVO() | BoardVO.builder() |
| Field setting | Uses setter methods | Uses builder methods |
| Immutability | Not immutable | Can be immutable if you remove setters |
| Readability (many fields) | Becomes messy and long | Clean and chainable |
| Required fields control | Hard to enforce | Easier with builder |
✅ Use builder when:
✅ Use setters when:
Let’s look at why setters can get messy, especially when dealing with many fields.
Let’s say your BoardVO has 6 fields:
BoardVO board = new BoardVO();
board.setWriter("테스터");
board.setTitle("Spring Study");
board.setContent("This is a test.");
board.setDate(LocalDate.now());
board.setHits(0);
board.setCategory("Dev");
💥 Even for just 6 fields, the code becomes a block of repetitive setter calls.
It’s harder to read and prone to mistakes (e.g., forgetting a setter or calling the wrong one).
If you forget to set a field:
BoardVO board = new BoardVO();
board.setWriter("테스터");
// forgot to set title, content, etc.
You now have an incomplete object, and this might cause bugs later.
There's no guarantee all required fields are set unless you manually check.
With builder:
BoardVO board = BoardVO.builder()
.writer("테스터")
.title("Spring Study")
.content("This is a test.")
.date(LocalDate.now())
.hits(0)
.category("Dev")
.build();
✅ You can see the full structure in one chain, like a filled-out form.
✅ It’s easier to spot what's missing, and the format is cleaner.
| Problem with Setters | Why it matters |
|---|---|
| Lots of repetitive code | Makes code hard to read and maintain |
| Risk of missing fields | Can lead to bugs or unstable state |
| Breaks object integrity | You create an empty object, then modify it |
| Doesn’t show intent clearly | Not obvious what values are final or required |
So it’s not that setters are bad — they’re just not ideal when you want: