✏️ 정적 멤버 클래스를 이용하여 DTO 관리하기

박상민·2023년 11월 18일

Spring

목록 보기
1/12
post-thumbnail

Java의 내부 클래스는 static으로 선언하자.

⭐️ DTO관리

공부한 내용을 복습하면서 토이 프로젝트 개발을 하고있는데 하나의 도메인에서 DTO가 여러개 나오게 되면서 클래스 파일도 많아졌다.
"이걸 좀 더 효율적으로 소스코드를 줄일 수 있는 방법은 없을까?" 고민을 했다.

그러던 도중 내부 클래스에 대한 글을 읽게되었고 정적 클래스를 이용해서 DTO를 관리하는 방법을 알게 되었다.

📌 중첩 클래스

public class TestClass {
	class NestedCLass {
    }
}
  • 클래스 안에 정의된 클래스를 중첩 클래스라고 부른다.
  • 하지만 중첩 클래스를 선언하면 static을 붙이라는 경고를 해준다.

📌 Staic

static을 붙이는 이유

  • static 내부 클래스로 선언하게 된다면 메모리 누수의 원인을 예방할 수 있고, 클래스의 각 인스턴스마다 더 적은 메모리를 사용한다.
  • static이 아닌 클래스는 바깥 인스턴스와 암묵적으로 연결이 되기 때문에 바깥 인스턴스 없이는 생성할 수 없다.
  • 두 클래스 관계는 클래스의 인스턴스 안에 만들어지며 메모리를 차지하게 되고 생성도 느리다.
  • 즉 멤버 클래스에서 바깥 인스턴스에 접근할 일이 없다면 static을 붙여 정적 클래스로 만들어주자

예시

public class UserResponse {

    /**
     * 회원 정적 멤버 클래스
     */
    @Getter
    @NoArgsConstructor(access = AccessLevel.PROTECTED)
    public static class UserDetailResponse {
        private String email;
        private String name;
        private String phone;
        private String birth;
        private String roadAddress;
        private String detailAddress;
        private String bankName;
        private String account;

        @Builder
        public UserDetailResponse(String email, String name, String phone, 
        			String birth, String roadAddress, String detailAddress, 
                    		String bankName, String account) {
            this.email = email;
            this.name = name;
            this.phone = phone;
            this.birth = birth;
            this.roadAddress = roadAddress;
            this.detailAddress = detailAddress;
            this.bankName = bankName;
            this.account = account;
        }

    }
    
    /**
     * 회원 정적 멤버 클래스로 변환하여 리턴합니다.
     * @param user 유저 엔티티
     * @return 변환된 유저 상세조회 DTO
     */
    public static UserDetailResponse toDetailUser(User user) {
        return UserDetailResponse.builder()
                .email(user.getEmail())
                .name(user.getName())
                .phone(user.getPhone())
                .birth(user.getBirth())
                .bankName(user.getBankName())
                .account(user.getAccount())
                .roadAddress(user.getUserAddress().getRoadAddress())
                .detailAddress(user.getUserAddress().getDetailAddress())
                .build();
    }

}

Controller

@GetMapping("/{userEmail}")
public UserResponse.UserDetailResponse getUser(@PathVariable("userEmail") String userEmail) {

		User findUser = userService.getUser(userEmail);
        return UserResponse.toDetailUser(findUser);

    }

출처
Java의 내부 클래스는 static으로 선언하자

0개의 댓글