Why Singletons are Evil

Tags:

http://blog.hanmir.com/sedere/254044
http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx

아.. msdn blog에는 트랙백 할줄을 모르겠고 (되긴 하는건가?),
한미르는 스크랩하기 이런거 밖에 없으니 트랙백 생략..

사실 msdn blog에는 트랙백 할 수 있어도 하기 겁나서 꺼려지는게 사실이긴 하지만..
뭐 어쨌든 트래백 어떻게 하는지 모르겠으니 핑계는 성립된 셈.

요약
1) Singletons frequently are used to provide a global access point for some service.
– 맞다. 근데 그렇게 매번 상태를 넘길 디자인이 필요없을걸?

2) Singletons allow you to limit creation of your objects.
– 맞다. 근데 그렇게 하면 비즈니스 로직 + 클래스 생성이라는 두개의 책임이 클래스에 주어지는데 이건 ‘하나의 클래스는 하나의 책임(responsibility)’이라는 룰을 어긴다. 차라리 빌더나 팩토리가 어때

3) Singletons promote tight coupling between classes
– 강한 결합은 테스트를 어렵게하며, 더군다나 싱글톤은 polymorphism 적용이 쉽지 않다(가 아니라 거의 불가능하지 않나?)

4) Singletons carry state with them that last as long as the program lasts
– 이것때문에 테스트가 힘들어진다.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *