簡體   English   中英

事件/操作機制的設計問題,Java generics

[英]Design Issue with Event/Action mechanism, Java generics

我目前的事件/動作機制設計遇到了問題。 有許多事件,可以通過某些標准來區分。 這會產生 class 層次結構。

public interface Eventpublic abstract class EventCategory implements Event和一個實際事件,例如public class ActualEvent extends EventCategory 然后是能夠根據給定事件執行操作的動作。

public interface Action<T extends Event> {
  void execute(T event);
}

並且實際的 Action ActualAction<T extends EventCategory> implements Action<T> 現在有生成特定實例的工廠,

public interface ActionFactory<T extends Event> {
  Action<T> create(String actionData);
}

public class ActualActionFactory implements ActionFactory<EventCategory> {
  @Override 
  Action<T> create(String actionData);
}

最后是EventConsumers,每個都有一個動作,將處理事件的發生並執行構造的動作。

public interface EventConsumer<T extends Event> {
  void handleEvent(T event);
}

public interface EventConsumerFactory<T extends Event> {
  EventConsumer<T> create(String data, Action<T> action);
}

協調器 class,它持有所有 ActionFactories 並委托 create 調用

public class Orchestrator {
 private final List<ActionFactory<? extends Event>> factories = Lists.newArrayList();
 private final List<EventConsumerFactories<? extends Event>> consumerFactories = Lists.newArrayList();
 
 public void createAction(String actionData) {
   for (ActionFactory<? extends Event> factory : factories) {
     Action<? extends Event> action = factory.create(actionData);
     if (action != null) {
       consumerFactories.forEach(cf -> cf.create(null, action));
     }
 }

我在最后一部分苦苦掙扎。 如果按上述方式鍵入 Action,則Action<? extends Event> Action<? extends Event>對 consumerFactories 的創建調用抱怨“需要 <capture of? extends Event>,提供 <capture of? extends Event>”。

我的 class 層次結構或模板邊界有什么問題? 如果在此位置對操作使用原始類型,則一切正常,並且為配置的事件執行操作。 但我想擺脫原始類型。

謝謝你的幫助!

親切的問候,安德烈亞斯

編輯:添加代碼如何注冊工廠

public interface EventConsumerFactory<T extends Event> {
  EventConsumer<T> create(String consumerData, Action<? super Event> action);
}
public class ActualEventConsumerFactory implements EventConsumerFactory<ActualEvent>  {
}

編排器注冊

  void registerConsumerFactory(ConsumerFactory<? super Event> factory) {
    this.consumerFactories.add(factory);
   }

這里的基本問題是您使用了錯誤的綁定類型。

EventConsumerAction接受T的實例:它們是消費者。 因此,您不想使用extends邊界聲明它們,因為您將無法向它們傳遞任何內容(除了文字null )。

如果您將這些界限更改為super ,您的代碼將編譯:

interface EventConsumerFactory<T extends Event> {
  EventConsumer<T> create(String data, Action<? super T> action);
                                             // here
}

class Orchestrator {
  private final List<ActionFactory<? super Event>> factories = Lists.newArrayList();
                                  // here
  private final List<EventConsumerFactory<? super Event>> consumerFactories = Lists.newArrayList();
                                         // here

  public void createAction(String actionData) {
    for (ActionFactory<? super Event> factory : factories) {
                      // here
      Action<? super Event> action = factory.create(actionData);
            // here
      if (action != null) {
        consumerFactories.forEach(cf -> cf.create(null, action));
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM