I’m searching for a way to make a library replaceable.
For example: I have a library which is managing communication over multicast . I’m using that library in my main application. My idea was to use an interface for that:
public interface MessageHandler {
public void sendMessage();
public Message receiveMessage();
}
Now it should be possible to change the library. For example a library which is managing communication over broadcast. Both are implementing the same interface.
So in my main application I want to have the opportunities:
MessageHandler mHandler = new MulticastImpl();
Or
MessageHandler mHandler = new BroadcastImpl();
My problem is: Where do I place the interface definition? When I place it into my main application I can’t see it from the library (because the library don’t know the main application).
When I place it into the both libraries I have two different MessageHandler-Interfaces (for example com.multicast.MessageHandler and com.broadcast.MessageHandler)
Any ideas?
This is a basic plugin system. what you should do is:
Note, there are more sophisticated ways to load plugins in java (e.g. using java.util.ServiceLoader and/or nested classloaders), but this is a good start.