3 ms·
If you are using spring (or any of the less cumbersome DI frameworks) this is exactly true. Generally in Java DI frameworks you make a bunch of factory methods
by wickawic 9y ago
If you are using spring (or any of the less cumbersome DI frameworks) this is exactly true. Generally in Java DI frameworks you make a bunch of factory methods for your logic classes(this may be magically done through annotations). A singleton in this approach is just a factory that always returns the same object.
- chubot 9y agoWhat I don't really understand is why you need a DI framework in Java. I use this style Python all the time with zero frameworks. You just make the objects in main() and make the objects in your test main(). Use helper functions if necessary. I do think Spring was very influential because I remember other people in the mid-2000's talking about testing, dependency injection, and Spring. I recall lots of people using Guice at Google but I never used it since I did very little Java. I feel like there is something about Java where you need a DI framework, and in Python you don't. I don't feel like you need one in C++ either. Or it might not even be possible to write one. I think LLVM is written in C++ in a style with zero globals. It's a library-based design.
- deleted 9y ago[deleted]
- dom0 9y agoPython has had closures for a long time, which make great ad-hoc factories that can capture state from upstream functions without making state visible along the way.
- chubot 9y agoYeah that can probably help. Another thing that helps is keyword arguments, so you avoid the verbose builder pattern that's idiomatic in Java. I was going to say that I don't use closures in Python all that much, and Python has some issues with closures (e.g. nonlocal is ugly, and it wasn't even there until Python 3). But after watching a recent PyCon talk [1], I realized that the "bound method" feature of Python is a useful special case of a closure. And he makes the good point that the code is shorter than JavaScript which requires an explicit anonymous function or bind(). [1] https://us.pycon.org/2017/schedule/presentation/768/ https://us.pycon.org/2017/schedule/presentation/768/
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]