开发者

Replace singletons with what?

Singletons are so often said开发者_JAVA技巧 to be a bad design choice, so how should you design an application when you want to avoid them?


you can use concepts such Dependency Injection to inject the services you depend on (basically the singletons you use) to you, instead of providing a global reference to them


My 2 cents.

Just design your application in such way that it's realy doesn't matter if it's singleton passed to your object or not.

Consider if you have MySingleton.Instance inside -> that looks bad and you are tightly coupled with this. If you passed MySignleton as a parameter in a method and it's instantiated outside as a MySingleton.Instance or new MySingleton() -> oh well, I still can mock or change it so it doesn't really matter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜