static factory method

μ§€ν˜œΒ·2025λ…„ 2μ›” 12일
0

πŸ’‘ https://github.com/cjh0412/Schedule_jpa

public static CommentResponseDto toDto(Comment comment){
        return new CommentResponseDto(
                comment.getId(),
                comment.getTodo().getId(),
                comment.getContent(),
                comment.getMember().getId(),
                comment.getMember().getUsername(),
                comment.getCreatedAt());
    } 

μ΄λ²ˆμ— jpaλ₯Ό μ΄μš©ν•œ 일정관리 ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€λ©΄μ„œ responseDto에 λ‹€μŒκ³Ό 같이 μ •μ λ©”μ„œλ“œλ₯Ό μƒμ„±ν•˜μ—¬ κ°œλ°œμ„ ν–ˆλ‹€.
그런데 μ–΄λ–€ λΆ„μ—κ²Œ static λ©”μ„œλ“œ μ‚¬μš© μ΄μœ κ°€ λ­”κ°€μš”? λΌλŠ” μ§ˆλ¬Έμ„ λ°›μ•˜λ‹€.
κ·Έλ‹Ήμ‹œ λ‚˜λŠ” λ©”μ„œλ“œλ₯Ό μƒμ„±ν•˜μ—¬ μ‚¬μš©ν•˜λ©΄ μ½”λ“œκ°€ κ°„κ²°ν•΄μ§„λ‹€λΌκ³ λ§Œ μ•Œκ³  μžμ„Ένžˆ μ•Œμ§€ λͺ»ν–ˆλ‹€.
λ•Œλ¬Έμ— μ’€ 더 μžμ„Ένžˆ κ³΅λΆ€ν•΄λ³΄κ³ μž ν–ˆλ‹€.

static λ©”μ„œλ“œ μž₯점

πŸ’‘ 1. 객체 생성 없이 호좜 κ°€λŠ₯

  1. μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€ μ•Šκ³  호좜이 κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— μ½”λ“œκ°€ 간결해짐
  2. 객체 μƒμ„±ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— λ©”λͺ¨λ¦¬κ°€ μ ˆμ•½

μ˜ˆμ‹œ

public MemberResponseDto save(CreateMemberCommand command) {
        if (memberRepository.findByEmail(command.getEmail()).isPresent()) {
            throw new MemberException(MemberErrorCode.ALREADY_REGISTER_MEMBER);
        }

        Member member = new Member(command.getUsername(), command.getEmail(), encoder.encode(command.getPassword()));
        memberRepository.save(member);
        
        // ❗ static λ©”μ„œλ“œ 없을 λ•Œ
        return new MemberResponseDto(member.getId(), member.getUsername(), member.getEmail());
        
        // ❗ static λ©”μ„œλ“œ μžˆμ„ λ•Œ
        return MemberResponseDto.toDto(member)
    }

πŸ’‘ 2. μž¬μ‚¬μš© κ°€λŠ₯ν•œ λ©”μ„œλ“œ 적합

  1. toDto() λŠ” 객체의 κ°’(μƒνƒœ)λ₯Ό μ§μ ‘μ μœΌλ‘œ 바꾸지 μ•Šκ³ , μž…λ ₯ 값을 λ°›μ•„ λ‹€λ₯Έ 객체에 λ°˜ν™˜ν•˜λŠ” μ—­ν• 
  2. νŠΉμ • 객체와 연관이 μ—†κΈ° λ•Œλ¬Έμ— μ‰½κ²Œ μž¬μ‚¬μš© κ°€λŠ₯

정적 method vs μΈμŠ€ν„΄μŠ€ method

πŸ’‘ 1. 정적 method

  • πŸ“Œ 객체 생성 없이 호좜 κ°€λŠ₯
  • μ—¬λŸ¬ κ³³μ—μ„œ μ‰½κ²Œ ν˜ΈμΆœν•˜μ—¬ μž¬μ‚¬μš©μ΄ 용이
  • 객체λ₯Ό μƒμ„±ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— λ©”λͺ¨λ¦¬ μ ˆμ•½
public MemberResponseDto save(CreateMemberCommand command) {
        if (memberRepository.findByEmail(command.getEmail()).isPresent()) {
            throw new MemberException(MemberErrorCode.ALREADY_REGISTER_MEMBER);
        }

        Member member = new Member(
                command.getUsername(),
                command.getEmail(),
                encoder.encode(command.getPassword())
                );
        memberRepository.save(member);
                
        // ❗ static λ©”μ„œλ“œ μžˆμ„ λ•Œ
        return MemberResponseDto.toDto(member)
    }
    
    
    public List<CommentResponseDto> findAll(){
        return commentRepository.findAll()
                .stream()
                // ❗ μž¬μ‚¬μš©κ°€λŠ₯!
                .map(CommentResponseDto :: toDto)
                .toList();
    }    

πŸ’‘ 2. μΈμŠ€ν„΄μŠ€ method

  • CommentResponseDto 객체λ₯Ό 생성해야 호좜 κ°€λŠ₯
  • λΆˆν•„μš”ν•œ 객체 생성이 λ°œμƒν•  수 있음
    @GetMapping("/{id}")
    public ResponseEntity<CommentResponseDto> findById(@PathVariable Long id){
        Comment comment = commentService.findById(id);
        return new ResponseEntity<>(
        // ❗ CommentResponseDto 객체 μƒμ„±ν•΄μ„œ μ‚¬μš©!
                new CommentResponseDto(
                        comment.getId(),
                        comment.getTodo().getId(),
                        comment.getContent(),
                        comment.getMember().getId(),
                        comment.getMember().getUsername(),
                        comment.getCreatedAt()), HttpStatus.OK);
    }

❓ 정적 λ©”μ„œλ“œλŠ” μ–΄λ–»κ²Œ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήλ κΉŒ?

μ—¬κΈ°κΉŒμ§€ μ•Œμ•„λ³΄λ‹ˆ 문득 κΆκΈˆν•¨μ΄ 생겼닀.
λ‚΄κ°€ μ§€κΈˆκΉŒμ§€ μ•ŒκΈ°λ‘œλŠ” 정적 λ©”μ„œλ“œλŠ” ν”„λ‘œμ νŠΈκ°€ μ‹œμž‘λ¨κ³Ό λ™μ‹œμ— λ©”λͺ¨λ¦¬μ— ν• λ‹Ήλ˜λŠ” κ²ƒμœΌλ‘œ μ•Œκ³  μžˆλŠ”λ° κ·Έλ ‡λ‹€λ©΄ responseDto값도 null이 λ§žλŠ” 게 μ•„λ‹κΉŒ?

κ²°λ‘ λΆ€ν„° λ§ν•˜μžλ©΄ NOμ˜€λ‹€.

πŸ“Œ 이유
1. static λ©”μ„œλ“œλŠ” ν”„λ‘œκ·Έλž¨ μ‹€ν–‰κ³Ό λ™μ‹œμ— λ©”μ„œλ“œ μ˜μ—­μ— 등둝
2. 이후 λ‚΄λΆ€ 둜직이 호좜되기 μ „κΉŒμ§€ μ‹€ν–‰λ˜μ§€ μ•ŠμŒ

βœ… κ²°λ‘ 
ν•΄λ‹Ή λ©”μ„œλ“œκ°€ 호좜 될 μ€€λΉ„κ°€ μ™„λ£Œλ˜μ—ˆλ‹€λŠ” 의미, μ‹€ν–‰λ˜λŠ” 것은 μ•„λ‹ˆλ‹€!
λ”°λΌμ„œ 값이 null이 λ‚˜μ˜€μ§€ μ•ŠλŠ”λ‹€!

static λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λŠ” 경우

πŸ’‘ 1. 객체 생성 없이 ν˜ΈμΆœν•΄μ•Ό ν•  λ•Œ

λ‹€μŒκ³Ό 같이 νŠΉμ • 객체와 관계 없이 λ™μž‘ν• λ•Œ static으둜 μ„ μ–Έ.

	System.out.println("객체 없이 호좜!!");

πŸ’‘ 2. 객체의 μƒνƒœλ₯Ό μœ μ§€ν•˜κ³  μž…λ ₯κ°’λ§Œ λ³€ν™˜ν•˜λŠ” 경우

βœ”οΈ 객체 생성 없이 μž…λ ₯값을 λ³€ν™˜ν•  λ•Œ
βœ”οΈ μž…λ ₯값을 μƒˆλ‘œμš΄ 객체둜 λ§Œλ“€μ–΄ λ°˜ν™˜ν•  λ•Œ

	public static CommentResponseDto toDto(Comment comment) {
    return new CommentResponseDto(
            comment.getId(),
            comment.getTodo().getId(),
            comment.getContent(),
            comment.getMember().getId(),
            comment.getMember().getUsername(),
            comment.getCreatedAt()
    );
}

πŸ’‘ 3. 곡톡 κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” 클래슀

βœ”οΈ 객체 생성 없이 μž…λ ₯값을 λ³€ν™˜ν•  λ•Œ
βœ”οΈ μ—¬λŸ¬ ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©λ  κ°€λŠ₯성이 μžˆμ„λ•Œ

    @ToString
    @Getter
    @AllArgsConstructor
    public static class ExceptionResponse{
        private CommentErrorCode errorCode;
        private String message;
    }

❌ static λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ μ•ˆ λ˜λŠ” 경우

λͺ¨λ“  λ©”μ„œλ“œμ— static을 λ‚¨μš©ν•˜λŠ”κ±΄ λ‹Ήμ—°νžˆ μ•ˆλœλ‹€!
κ·Έλ ‡λ‹€λ©΄ μ–΄λ–¨ λ•Œ static λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ μ•ˆλ κΉŒ?

  • 객체의 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ 값을 λ³€κ²½ν•΄μ•Ό ν•˜λŠ” 경우
  • 상속을 톡해 λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ“œν•΄μ•Ό ν•˜λŠ” 경우
  • κ°μ²΄λ§ˆλ‹€ λ‹€λ₯Έ λ™μž‘μ„ ν•΄μ•Ό ν•˜λŠ” 경우
public class Caculator {
    private int count = 0;

    public static void add(int num1, int num2) {
        count = num1+num2; // ⚠ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ μ‚¬μš© λΆˆκ°€!
    }     
}

βœ… κ²°λ‘ 

ν•˜λ‚˜μ˜ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ”λΌλ„ μ’€ 더 μžμ„Ένžˆ 곡뢀λ₯Ό ν•  ν•„μš”μ„±μ΄ μžˆλ‹€.
static은 잘 μ‚¬μš©ν•˜λ©΄ 보닀 쒋은 κ°œλ°œμ„ ν•  수 μžˆμ§€λ§Œ λ„ˆλ¬΄ λ‚¨μš©ν•˜λ©΄ μ—λŸ¬ 폭탄 + λ©”λͺ¨λ¦¬ λ‚­λΉ„κ°€ λ°œμƒν•œλ‹€. 잘 μƒκ°ν•˜κ³  μ‚¬μš©ν•˜μž!

일정관리 ν”„λ‘œκ·Έλž¨ νŠΈλŸ¬λΈ”μŠˆνŒ…

0개의 λŒ“κΈ€

κ΄€λ ¨ μ±„μš© 정보