Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • I think PieterV is saying he wants to use dependency injection so that he can mock in the test case, but in the non-test case he can't create the sockets ahead of time because of wasting resources. I don't think he's concerned about the mocks wasting resources. Commented Oct 27, 2022 at 14:11
  • @BenAaronson if that's true then again, see the Abstract Factory Pattern. You can think of it like a button that constructs on demand whatever thingy you need without having to know what exactly the thingy is. All you need to know is when you need it. DI doesn't have to all happen in main. If it did you'd have no timestamps. Just get as high up the call stack as you can get. Commented Oct 27, 2022 at 14:18
  • Also exist the concept Provider or Supplier . Instead of injecting the dependency, we inject a component that will sort which instance must be used when provider.get() . But if you can't create the instances ahead of time, then the factory seems better solution. Commented Oct 27, 2022 at 15:01
  • @Laiv I think I know what you're talking about but Google isn't cooperating. Know a good link to cite? Commented Oct 27, 2022 at 15:08
  • My idea of Supplier comes from Java Stream supplier functions. The Provider comes from an approach to Delta Spike's BeanProvider, Both have in common that the "strategy" to choose which bean/data to return is encoded in one of these components. It's solved in runtime and not necessarily during bootstrapping. Commented Oct 27, 2022 at 15:19