我正在尋找一種方法來使庫可替換。例如:我有一個管理多播通信的庫。我在我的主應用程序中使用該庫。我的想法是使用以下接口:如何使庫在java中可替換?
public interface MessageHandler {
public void sendMessage();
public Message receiveMessage();
}
現在應該可以更改庫。例如一個管理廣播通信的庫。兩者都在實施相同的界面。
所以在我的主要應用程序,我希望有機會:
MessageHandler mHandler = new MulticastImpl();
或者
MessageHandler mHandler = new BroadcastImpl();
我的問題是:我在哪裏放置的接口定義?當我把它放到我的主應用程序中時,我無法從庫中看到它(因爲庫不知道主應用程序)。 當我把它放到兩個庫中時,我有兩個不同的MessageHandler-接口(例如com.multicast.MessageHandler和com.broadcast.MessageHandler)
任何想法?