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 ?