Why use singleton instead of static class?

前端 未结 7 729
迷失自我
迷失自我 2020-12-08 04:02

When would a singleton actually be easier or better than a static class? It seems to me creating a singleton is just extra effort that\'s not actually needed, but I\'m sure

7条回答
  •  眼角桃花
    2020-12-08 04:36

    Singletons preserve the conventional class approach, and don't require that you use the static keyword everywhere. They may be more demanding to implement at first, but will greatly simplify the architecture of your program. Unlike static classes, we can use singletons as parameters or objects.

    Also,you can use singletons with interfaces just like any other class.

提交回复
热议问题