3

Quoting the definition of interface injection from Wikipedia :

The advantage of interface injection is that dependencies can be completely ignorant of their clients yet can still receive a reference to a new client and, using it, send a reference-to-self back to the client. In this way, the dependencies become injectors

I wish to understand each part of what is said there.

Let me put the example here from the mentioned source:

// Service setter interface.
public interface ServiceSetter {
    public void setService(Service service);
}

// Client class
public class Client implements ServiceSetter {
    // Internal reference to the service used by this client.
    private Service service;

    // Set the service that this client is to use.
    @Override
    public void setService(Service service) {
        this.service = service;
    }
}

Now let me split the quotation in parts and try to explain them :

part1 : "..dependencies can be completely ignorant of their clients.." - quite understandable

part2: "..dependencies can be completely ignorant of their clients yet can still receive a reference to a new client and, using .. " - bold part is obscure. Q1) Client gets a reference to the dependency not the vice versa, right ?

part3: "..can still receive a reference to a new client and, using it, send a reference-to-self back to the client..." - quite understandable

part4: ".. In this way, the dependencies become injectors.."- bold part is obscure. Q2) Dependencies are being injected. So how can they themselves be injectors ?

Q3) Dependency is obviously injected in the example. Can we say that interface is also being injected only to enable the injection of the dependency in the example ?

  • 1
    To the downvoters: if you don't like the question, you had time enough to tell the OP about your concerns in this former meta post. So if you still want to downvote now, please leave a comment (or stay away). – Doc Brown Jan 30 at 6:58
3

part2: "..dependencies can be completely ignorant of their clients yet can still receive a reference to a new client and, using .. " - bold part is obscure. Q1) Client gets a reference to the dependency not the vice versa, right?

Think of the term "dependency" here not just of the Service class itself, but of a library or framework where this class is located. Further assume for a moment that the Service object cannot be constructed at construction time of the Client object for some reason. The "interface injection" then makes it possible for the library to provide a function (maybe in some kind of factory or builder class) which gets the formerly constructed client object (as a ServiceSetter implementation), constructs the Service object and injects the Service object at the point in time when it is possible.

In pseudo code:

// assume this class is located in the same framework
// where "Service" lives
public class ServiceInjector
{
     public void Inject(ServiceSetter client)
     {
           // ...
           // maybe some reusable framework logic
           // to derive construction parameters 
           // for new service here
           // ..
           Service service = new Service(/*...*/);
           client.setService(service);
     }
}

So the "Dependency" - the library where ServiceInjector and Service are located - does not have know the concrete client object, still ServiceInjector can inject a Service object into the client.

(Assume the client object has to be constructed earlier, maybe it is needed first for making it possible to decide if Inject is ever called. Then "constructor injection" would not be possible here.)

part4: ".. In this way, the dependencies become injectors.."- bold part is obscure.

This fits perfectly to what I wrote above: if you think the term "dependency" here refers to the full framework/library, the function I described is such an "injector".

And yes, I agree, the usage of the term "dependency" with slightly different meanings in mind does not make the Wikipedia article very readable.

  • can you provide at least some level of pseudo code to understand the answer ? – Istiaque Ahmed Jan 30 at 7:11
  • @IstiaqueAhmed: you are welcome – Doc Brown Jan 30 at 7:27
  • can you give the example in code to explain what you said ? – Istiaque Ahmed Jan 31 at 14:40
  • @IstiaqueAhmed: can you please give us a hint first where you precisely have problems to follow what I already wrote? – Doc Brown Jan 31 at 17:05
  • From Wikipedia -the dependency's interface provides an injector method that will inject the dependency into any client passed to it.- This might help to clarify Doc's understanding of "dependency" here. If "dependency" was a single class, it could not provide us with an interface and an injector at the same time without breaking a handful of good design principles. It led me to think in dependency as "a whole" or, being more accurate, as a component or library. Indeed, Wikipedia's use for "dependency" is somewhat vague or imprecise. – Laiv Mar 5 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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