inblog logo
|
LifeLog, DevLog
    Java

    Lombok과 Record 의 사용에 관하여

    KYJTHEYJ's avatar
    KYJTHEYJ
    Dec 24, 2025
    Lombok과 Record 의 사용에 관하여
    Contents
    Lombok 을 쓰는 경우Record 를 쓰는 경우

    Lombok 을 쓰는 경우

    • 가변/불변 모두 가능

    • JPA Entity

    • Builder 패턴

      class complexConstructorClass {
       String title1;
       String title2;
       String title3;
       String content1;
       int content1Number1;
       .....
      
       // 너무나도 길어짐
       complexConstructorClass(String title1, ...2 ..3 ...) {
        ....
       }
      }
      
      // 이런 경우에 내부에 static 클래스를 통하여
      
      complexConstructorClass ccc = complexConstructorClass.builder()
      .title1(...)
      .title2(...)
      .....
      .build();
      
      // 이런 구조로 객체 생성을 하여 복잡성에 대해 명확과 정확성을 부여하는 패턴
      
      //----------------------------------------------------------
      
      // 예시
      public class User {
          private final String name;
          private final String email;
          private final int age;
          private final String phone;
          
          // private 생성자 (외부에서 직접 생성 불가)
          private User(Builder builder) {
              this.name = builder.name;
              this.email = builder.email;
              this.age = builder.age;
              this.phone = builder.phone;
          }
          
          public static class Builder {
              private String name;
              private String email;
              private int age;
              private String phone;
              
              public Builder name(String name) {
                  this.name = name;
                  return this;
              }
              
              public Builder email(String email) {
                  this.email = email;
                  return this;
              }
              
              public Builder age(int age) {
                  this.age = age;
                  return this;
              }
              
              public Builder phone(String phone) {
                  this.phone = phone;
                  return this;
              }
              
              public User build() {
                  return new User(this);
              }
          }
          
          public static Builder builder() {
              return new Builder();
          }
          
          // Getter들
          public String getName() { return name; }
          public String getEmail() { return email; }
          public int getAge() { return age; }
          public String getPhone() { return phone; }
      }
      
      // 사용
      User user = User.builder()
          .name("홍길동")
          .email("hong@example.com")
          .age(30)
          .build();  // phone은 생략 가능!
      
      //----------------------------------------------------------
      
      // Lombok을 사용하면 더 쉬워짐
      @Getter
      @Builder
      public class User {
          private String name;
          private String email;
          private int age;
          private String phone;
      }
      
      // 사용 (똑같이 동작!)
      User user = User.builder()
          .name("홍길동")
          .email("hong@example.com")
          .age(30)
          .build();
    • 로깅

    • 복잡한 equal/hashcode 구성시

    Record 를 쓰는 경우

    • 불변의 경우만 사용

    • DTO

    • 값 객체

    • 이벤트

    • 쿼리의 결과

    💡

    둘을 명확한 용도로 복합적으로 사용하는 것이 제일 좋다

    Share article
    Contents
    Lombok 을 쓰는 경우Record 를 쓰는 경우

    LifeLog, DevLog - https://github.com/KYJTHEYJ

    RSS·Powered by Inblog