Builder vs setter

Yeeun·2025년 4월 17일
0

SpringBoot

목록 보기
3/46

the difference between:

board.setWriter("테스터");

and

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

Let’s break it down gently and clearly:


🟦 1. setWriter("테스터") – Traditional Way

This is the normal setter method used when:

  • You create an object using new
  • Then set values using mutator (setter) methods

Example:

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

👉 This changes the object after it's created.


🟩 2. .builder().writer("테스터").build(); – Builder Pattern

This is the builder pattern, used when:

  • You want to set values at the time of creation
  • You want your object to be immutable (no setters)

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.


🔍 So what's the difference?

FeaturesetWriter()builder().writer().build()
Object creationnew BoardVO()BoardVO.builder()
Field settingUses setter methodsUses builder methods
ImmutabilityNot immutableCan be immutable if you remove setters
Readability (many fields)Becomes messy and longClean and chainable
Required fields controlHard to enforceEasier with builder

✅ When to use which?

  • ✅ Use builder when:

    • The class has many fields
    • You want to control object creation more clearly
    • You want immutability
    • You're using Lombok
  • ✅ Use setters when:

    • It’s a small object
    • You're okay with changing values later

Let’s look at why setters can get messy, especially when dealing with many fields.


🟫 1. Setter code gets long and repetitive

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).


🟩 2. Setters allow partial or inconsistent objects

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.


🟨 3. Harder to read the full structure at once

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.


🧼 Summary: Why setters get messy

Problem with SettersWhy it matters
Lots of repetitive codeMakes code hard to read and maintain
Risk of missing fieldsCan lead to bugs or unstable state
Breaks object integrityYou create an empty object, then modify it
Doesn’t show intent clearlyNot obvious what values are final or required

So it’s not that setters are bad — they’re just not ideal when you want:

  • Clean, readable object construction
  • Immutability or safety
  • Fewer bugs from missing fields

0개의 댓글