Purpose of singletons in programming

后端 未结 9 1689
傲寒
傲寒 2020-12-08 01:49

This is admittedly a rather loose question. My current understanding of singletons is that they are a class that you set up in such a way that only one instance is ever crea

9条回答
  •  暖寄归人
    2020-12-08 02:42

    why you would wan't to

    I wouldn't because singletons usually are very bad way to solve your problems. My recommendation to you is to avoid them completely.

    The main reasons are:

    • Singletons mostly represent global state (which is evil).
    • Correct dependency injection becomes impossible.

    I suggest you read the rest (including thorough explanations) in this Google employee's blog:

    • http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
    • http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/
    • http://misko.hevery.com/2008/08/25/root-cause-of-singletons/
    • http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/

提交回复
热议问题