Reputation: 4730
In the code of the project I'm working on I encountered a strange approach.
The UI layer gets dependecies using a sort of Service Locator which is a class with static methods:
public class ServiceManager {
public static MailService getMailService() {
...
}
public static UserInfoService getUserInfoService() {
...
}
...
}
The dependencies that are "distributed" by this class are injected to it using Spring framework.
What could be the reason for this approach? I can see only downsides. Since the locator methods are static, there is no interface. The absence of interface makes it harder to reason about the purpose of the class. Clients of this class are tightly coupled to it (remember, no interface there), making them impossible to reuse elsewhere.
Wouldn't it be much better to let Spring inject dependencies in the UI classes directly?
Upvotes: 1
Views: 249
Reputation: 172666
With most UI frameworks, it is often very hard (if not impossible) to use constructor injection in the UI classes. In that case it is common to revert to the Service Locator pattern, but only -I repeat only- in the UI classes.
Upvotes: 2