-
22. 스태틱 vs 싱글톤 (Static vs Singleton)스프링개발자/201 - 일반 2020. 10. 29. 10:43
오늘 회사에서 코드리뷰를 받다가, 해당 클래스가
Spring Component(Singleton)인데, 왜 그 안에 static method를 썼냐고 질문을 받았다.
스태틱과 싱글톤에대해 생각해본 적이 거의 없어서, 알아보았는데, 정리된 내용을 여기에 적는다.
먼저, 스태틱과 싱글톤은 비교는 apple to apple의 올바른 비교의 대상이다.
스태틱 클래스는 스태틱 메소드만을 구현 할 수 있다.
인스턴스를 만들 수 없고, 생성자도 갖지 않으며, 거의 Utility 성 함수의 모음 같은 역할이다.
성능도 스태틱이 빠르다.
싱글톤에서는 OOP의 장점을 살려서 interface, override, lazy loading 등등 프로그램적으로
활용할 수 있는 여지가 많다.
결론적으로 @Component 에서는 static method를 쓰지말자.
stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern
'스프링개발자 > 201 - 일반' 카테고리의 다른 글
21. MongoDB를 꼭 써야해? (0) 2020.10.19 20. @Scheduled 어노테이션 (0) 2020.10.17 19. 자바 인터페이스 with code body (0) 2020.10.02 18. Git 브랜치 프로텍션(Branch Protection Rule) (0) 2020.09.07 17. 자바 스트림 (0) 2020.09.07