CS FundamentalsHardProduct-company placement

A class gets its dependencies by calling ServiceLocator.get('Mailer') inside its methods, instead of receiving them through its constructor. What is the main drawback compared with dependency injection?

  1. A. Each call needs run-time reflection, so the code can no longer be compiled ahead of time
  2. B. Needs are hidden in method bodies, and tests must configure the global locator
  3. C. It creates a new Mailer on every call, so shared state between calls becomes impossible
  4. D. It blocks swapping in a fake Mailer, since the locator only ever returns real objects

Correct answer: B. Needs are hidden in method bodies, and tests must configure the global locator

A service locator still decouples from concrete classes and fakes can be registered, but every class quietly depends on the locator and its real needs show up only at run time. Constructor injection makes them explicit.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic