2

I'm having a problem with a library I'm currently writing in Java. I've created an interface in my library which i need the user of this lib to implement. From within my library I'm calling the implementation (why is irrelevant).

At first I was using CDI and just injected my interface into the controlling class in my lib like this

@Inject
private Interface interface;

And i relied on the container to find the implementation made by the user of said lib. But I'm thinking of switching to SE since the limitation of using Java EE might be blocking for some developers.

As you can see I'm facing the problem that I cannot instantiate my interface without knowing the implementation class and cannot call the methods in the implementation class...

Any idea on how i could fix this problem?? (I have an idea of my own but I want something better, I'm not putting it in this post so your opinion isn't influenced @:))

Thanks in advance.

Edit: example: This is my controller class

public class Controller{
     private void InterfaceToImplement interfaceToImplement;

     public void something(){
        String s = interfaceToImplement.someFunction();
     }
}

This is my interface

public interface {
     abstract String someFunction();
}

The user of my lib is reponsible for implementing my interface and implementing the someFunction method. I'm calling this function from my controller class. I cannot call an interface function if the object is not initialized. With CDI, the container finds the implementation and injects it into the interface object in my controller class, thus it is initialized and i can call the user's implementing method. With SE i cannot do such thing... And since i do not know what is implementing my interface, I'm kinda stuck on this.

Hope it is more clear now :).

4

1 回答 1

0

JDBC呢?它有类似的问题。您在那里在运行时提供实现。

于 2013-08-14T14:17:00.263 回答