2 ms·
Singletons, globals/statics, and dependency injection all seem to be solutions to the problem of making objects find each other at runtime. Maybe it's time for
by cousin_it 14y ago
Singletons, globals/statics, and dependency injection all seem to be solutions to the problem of making objects find each other at runtime.
Maybe it's time for a dedicated language feature to solve that problem? For example, each object could carry an implicit reference to an "object pool", from which it can fetch other objects by their class/interface. If an object constructs other objects, the "pool" gets passed along implicitly. Test suites could use "pools" filled with mocks. And unless I'm missing something, the presence of all required dependencies could be statically checked at compile time.
Basically it would be like Lisp's dynamic variables, but based on the construction hierarchy instead of the call hierarchy. What do you think?