Problems with Singleton Pattern

后端 未结 7 582
渐次进展
渐次进展 2020-11-27 12:16

I\'ve been reading about Singleton pattern for last few days. The general perception is that the scenarios where it is required are quite few (if not rare) probably because

7条回答
  •  孤独总比滥情好
    2020-11-27 12:46

    About this unit testing concern. The main problems seems to be not with testing the singletons themselves, but with testing the objects that use them.

    Such objects cannot be isolated for testing, since they have dependencies on singletons which are both hidden and hard to remove. It gets even worse if the singleton represents an interface to an external system (DB connection, payment processor, ICBM firing unit). Testing such an object might unexpectedly write into DB, send some money who knows where or even fire some intercontinental missiles.

提交回复
热议问题