Skip to content

아이템 1. 생성자 대신 정적 팩터리 메서드를 고려하라 #17

Discussion options

You must be logged in to vote
  1. 정적 팩터리 메서드를 작성하는 시점에는 반환할 객체의 클래스가 존재하지 않아도 된다.

5번에서는 갑자기 낯선 용어들의 나열이라,, 많이 어렵게 느껴졌는데요
용어는 차치하고 java에서 db랑 연결할 때의 방식을 이해하면 될 거 같습니다
https://sysgongbu.tistory.com/m/95
이 블로그에 잘 나와있는데 제가 이해한바로는 Class.forName에서 string으로 com.mysql.jdbc.Driver (FQCN) 를 받아서 동적으로 클래스 로딩을 하고 있기 때문에, 코드를 작성하는 시점에는 없더라도 추후에 해당 클래스를 만들어서 추가해두면 된다 라고 이해했네요

정리하자면 정적 팩터리 메서드를 작성할 때는 단순 string으로,, (FQCN) 으로 리플렉션 API를 통해서 해당 클래스를 로딩해서 사용할 것이다만 명시해두고 (실제로 해당 클래스가 존재하든 아니든) 이후에 실제 구현부인 클래스를 작성하는 형태로도 가능하다.
https://www.youtube.com/watch?v=X7RXP6EI-5E&list=PLfI752FpVCS8e5ACdi5dpwLdlVkn0QgJJ

이 영상 참고해도 좋습니다.

Replies: 6 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@YuDeokRin
Comment options

Comment options

You must be logged in to vote
2 replies
@Irisation23
Comment options

Irisation23 Dec 19, 2022
Maintainer Author

@jinan159
Comment options

Answer selected by Irisation23
Comment options

You must be logged in to vote
1 reply
@YuDeokRin
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2장 객체 생성과 파괴 이펙티브 자바 2장 (객체 생성과 파괴)
4 participants