Are there any viable alternatives to the GOF Singleton Pattern?

后端 未结 16 1466
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 16:58

Let\'s face it. The Singleton Pattern is highly controversial topic with hordes programmers on both sides of the fence. There are those who feel like the Singleto

16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 17:34

    Alex Miller in "Patterns I Hate" quotes the following:

    "When a singleton seems like the answer, I find it is often wiser to:

    1. Create an interface and a default implementation of your singleton
    2. Construct a single instance of your default implementation at the “top” of your system. This might be in a Spring config, or in code, or defined in a variety of ways depending on your system.
    3. Pass the single instance into each component that needs it (dependency injection)

提交回复
热议问题