0

Let me first describe the situation

I have a component that uses dependency injection through a service locator. In the first scenario (image 1) the component needs a class that implements Interface0, so it just asks for it at the service locator, something like this (in pseudocode):

Inferface0 i0 = serviceLocator.get(Interface0);

single interface and implementation

This first scenario is very simple. Now the second scenario, where the component and service locator stays the same, but now the component needs a class that implements Interface1 and Interface2, like image 2 shows.

multiple interfaces and implementations

The question is, there is needed to create a new interface (let's call it Interface4) that implements Interface1 and Interface2 so I can ask for it like following?

Inferface4 i4 = serviceLocator.get(Interface4);

And then, if later the component needs an implementation of Interface2 and Interface3 then a creation of Interface5 would be needed? What is the best approach to handle this interfaces?

2
  • Side note: service locator often considered anti-pattern, so there is really no need for you to mention that in the question - there is no difference between asking this question for service locator or constructor ingestion. While it was not (yet?) case here, showing code/concepts that are not needed for the question but controversial (or outright frowned upon like SQL injection sample code or parsing HTML with regex) - may derail questions by flood of comments on such concepts instead of the main topic. Mar 28 at 22:07
  • Why do you need to merge these interfaces? Why not just ask for both an Interface1 and an Interface2 dependency? What are you trying to achieve by stacking this into a single Interface4 request from the service locator? It's not like you're being charged per request, are you?
    – Flater
    Mar 28 at 23:40

2 Answers 2

2

The question is, there is needed to create a new interface (let's call it Interface4) that implements Interface1 and Interface2 so I can ask for it like following?

If you want to receive a single object that implements both interfaces, you probably also want to store a single reference to that object. If you declare your reference as Interface1 iA, then you cannot access the Interface2 methods without ugly typecasting tricks and the other way around when you declare it as Interface2 iA.

The only way to declare a reference of a type that can access both Interface1 and Interface2 methods is to create a Interface4 that combines the two interfaces. And your implementation classes would also have to declare they implement Interface4.

And then, if later the component needs an implementation of Interface2 and Interface3 then a creation of Interface5 would be needed? What is the best approach to handle this interfaces?

If you regularly have this problem, you need to critically re-evaluate how you determine which interfaces you need. Maybe you should not have created Interface1, Interface2, and Interface3 at all, but only Interface4 and Interface5.

1

Well, you could do that. Or:

Interface1 i0 = new Implementation1();

Just do that high up the call stack (say in main maybe) and pass it into something that needs it. That thing can use it as an object that implements Interface1 without knowing or caring that it happens to be Implementation1.

Creating Inferface4 is only really justified if something uses the object that way. At best, creating interfaces just to make a service locator happy is a hack. Interfaces and objects (even singleton objects) were never meant to have an exclusively 1 to 1 relationship. Interfaces are meant to express what the client (using) code needs.

It's also worth noting that interfaces aren't the only kind of key your service-locator/container/map can use. So if your code base doesn't naturally have that 1 to 1 relationship you don't have to force it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.