@Setter 지양

2024. 9. 11. 15:10·develop

image

Setter 메소드를 사용하면 안 되는 이유

  • 값 변경의 의도를 파악하기 어렵다
  • Setter 메소드를 사용하면 객체의 값을 변경한 의도를 명확하게 알기 어렵습니다.
  • 객체의 일관성을 유지하기 어렵다
  • Setter는 public으로 선언되어, 외부에서 언제든지 객체의 상태를 변경할 수 있어 객체의 일관성을 유지하기 어렵습니다.

1. 값 변경의 의도를 파악하기 어렵다

public Book updateBook(long id) {
    final Book book = findById(id);
    book.setTitle("New Title");
    book.setAuthor("New Author");
    book.setPublishedYear(2023);
    book.setGenre("Science Fiction");
    return book;
}

위 코드는 Book 객체의 정보를 변경하는 예시입니다. 하지만 setter 메소드들이 나열되어 있을 뿐, 변경의 의도가 명확하지 않아 코드의 가독성이 떨어집니다.


2. 객체의 일관성을 유지하기 어렵다

Setter 메소드는 외부에서 객체의 상태를 언제든지 변경할 수 있기 때문에, 객체의 일관성을 해칠 위험이 있습니다. 특히, 도서 정보 관리에서 모든 곳에서 객체의 상태를 변경할 수 있다면 데이터 무결성이 유지되지 않을 수 있습니다.


Setter 대신 사용할 수 있는 방법

  • 생성자 오버로딩
  • Builder 패턴
  • 정적 팩토리 메소드

1. 생성자 오버로딩

public class Book {
    private String title;
    private String author;
    private int publishedYear;
    private String genre;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public Book(String title, String author, int publishedYear) {
        this.title = title;
        this.author = author;
        this.publishedYear = publishedYear;
    }
}

생성자를 오버로딩하여 객체를 생성하는 방법입니다. 하지만 멤버 변수가 많아지면 생성자 코드가 복잡해져 가독성이 떨어질 수 있습니다.

[주의] JPA 사용 시 기본 생성자가 필요합니다.


2. Builder 패턴

public class Book {
    private String title;
    private String author;
    private int publishedYear;
    private String genre;

    @Builder
    public Book(String title, String author, int publishedYear, String genre) {
        this.title = title;
        this.author = author;
        this.publishedYear = publishedYear;
        this.genre = genre;
    }
}

Builder 패턴을 사용하면 필요한 데이터만으로 유연하게 객체를 생성할 수 있습니다. 이는 다양한 생성자들 대신 하나의 통합된 형태로 관리할 수 있어 유지보수 및 가독성이 향상됩니다. 또한, 인자의 순서를 상관하지 않기 때문에 실수를 줄일 수 있습니다.


3. 정적 팩토리 메소드

public class Book {
    private String title;
    private String author;
    private int publishedYear;
    private String genre;

    public static Book createBook(String title, String author, int publishedYear, String genre) {
        Book book = new Book();
        book.title = title;
        book.author = author;
        book.publishedYear = publishedYear;
        book.genre = genre;
        return book;
    }
}

정적 팩토리 메소드를 사용하면 메소드에 이름을 부여하여, 반환할 객체의 의도를 더욱 명확히 할 수 있습니다.


활용

정적 팩토리 메소드 + builder

 @Builder
    private Book(String title, String author, int publishedYear, String genre) {
        this.title = title;
        this.author = author;
        this.publishedYear = publishedYear;
        this.genre = genre;
    }

    /* 생성 */
    public static Book createBook(String title, String author, int publishedYear, String genre) {
        return Book.builder()
                .title(title)
                .author(author)
                .publishedYear(publishedYear)
                .genre(genre)
                .build();
    }

private로 선언된 내부 builder 를 이용하여 유연하게 정적 팩토리 메소드를 생성하는 방법

entity class 내 코드의 가독성이 높다는 장점이 있다.

'develop' 카테고리의 다른 글

교차 출처 리소스 공유 CORS 정리  (0) 2024.09.30
언제 RDB 대신 MongoDB를 사용할까  (1) 2024.09.26
CQS (Command Query Separation)  (0) 2024.09.11
Spring Rest Docs를 통한 API 문서화  (3) 2024.09.10
Velog -> Tistory 블로그 이전  (0) 2024.09.09
'develop' 카테고리의 다른 글
  • 언제 RDB 대신 MongoDB를 사용할까
  • CQS (Command Query Separation)
  • Spring Rest Docs를 통한 API 문서화
  • Velog -> Tistory 블로그 이전
VANEL
VANEL
break;
  • VANEL
    VANEL의 블로그
    VANEL
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 오류
      • develop
      • 과거 기록
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 이전 블로그
  • 공지사항

  • 인기 글

  • 태그

    Spring
    spring security
    WebRTC
    coturn
    코드리뷰
    restdocs
    JWT
    테스트코드
    Spring boot
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
VANEL
@Setter 지양
상단으로

티스토리툴바