我有一個類:使用「this.getClass()。getMethod()」檢查泛型類型是不是個好主意?
public class EventListener<T extends Event> {
private final EventHandler<T> handler;
public EventListener(EventHandler<T> handler) {
if (handler == null) {
throw new NullPointerException();
}
this.handler = handler;
}
public boolean isApplicable(Event event) {
try {
this.getClass().getMethod("onEvent", event.getClass());
return true;
} catch (NoSuchMethodException | SecurityException ex) {
return false;
}
}
public void onEvent(T event) {
handler.handle(event);
}
}
中的onEvent(T事件)方法,我需要調用isApplicable(事件event)方法來檢查監聽器可以處理此事件的調用之前。
我不想增加這種方式的一些構造函數的參數:
public class GenericClass<T> {
private final Class<T> type;
public GenericClass(Class<T> type) {
this.type = type;
}
public Class<T> getMyType() {
return this.type;
}
}
聽衆爲不同的事件將被存儲在相同的列表。所以我需要一些工具來檢查監聽器是否可以處理列表中的事件。
public class EventService extends Service {
private final ArrayList<EventListener> listeners = new ArrayList<>();
public EventService(ServiceContainer serviceContainer) {
super(serviceContainer);
}
public ArrayList<EventListener> getListeners() {
return listeners;
}
public <T extends Event> void trigger(T event) {
listeners.stream().forEach((listener) -> {
if (listener.isApplicable(event)) {
listener.onEvent(event);
}
});
}
}
是否有一個更優雅的代碼,把這個方法:
public boolean isApplicable(Event event) {
try {
this.getClass().getMethod("onEvent", event.getClass());
return true;
} catch (NoSuchMethodException | SecurityException ex) {
return false;
}
}
以事件的接口中聲明的onEvent方法,或使抽象和定義的onEvent。這樣,就沒有必要檢查isApplicable或創建一個新的接口並在那裏聲明onEvent。所有聽衆都應該實現這個新界面 – Jaiprakash
究竟是什麼*是你的問題?你已經發布的代碼將無法用於所有派生類的'Event',除非已經提供了該派生類的適當的'onEvent()'方法重載。這是意圖嗎? – EJP
我在這個問題上做了一些澄清。感謝回覆。結果添加爲標記爲「最後:」的答案。 –