Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
事件/动作机制的设计问题,Java泛型_Java_Templates_Generics - Fatal编程技术网

事件/动作机制的设计问题,Java泛型

事件/动作机制的设计问题,Java泛型,java,templates,generics,Java,Templates,Generics,我目前设计的事件/行动机制存在问题。有许多事件可以通过某些标准加以区分。这将导致类层次结构 公共接口事件,公共抽象类EventCategory实现事件和实际事件,例如公共类ActualEvent扩展了EventCategory。然后是能够基于给定事件执行操作的操作 public interface Action<T extends Event> { void execute(T event); } 公共接口操作{ 无效执行(T事件); } 实际操作实际操作执行操作。 现在有工

我目前设计的事件/行动机制存在问题。有许多事件可以通过某些标准加以区分。这将导致类层次结构

公共接口事件
公共抽象类EventCategory实现事件
和实际事件,例如
公共类ActualEvent扩展了EventCategory
。然后是能够基于给定事件执行操作的操作

public interface Action<T extends Event> {
  void execute(T event);
}
公共接口操作{
无效执行(T事件);
}
实际操作
实际操作执行操作
。
现在有工厂生成特定的实例

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

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

公共接口操作工厂{
动作创建(字符串actionData);
}
公共类ActualActionFactory实现ActionFactory{
@凌驾
动作创建(字符串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);
}
公共接口事件消费者{
无效处理事件(T事件);
}
公共接口EventConsumerFactory{
EventConsumer创建(字符串数据、操作);
}
orchestrator类,它保存所有ActionFactory并委托创建调用

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));
     }
 }
公共类编排器{

private final List这里的基本问题是使用了错误的绑定类型

EventConsumer
Action
都接受
T
的实例:它们是消费者。因此,您不希望使用
extends
边界声明它们,因为您将无法向它们传递任何内容(除了literal
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));
      }
    }
  }
}
接口事件ConsumerFactory{

EventConsumer创建(字符串数据,操作消费者不应该有
扩展
通配符:请参阅。(旁白:
操作
EventConsumer
之间的区别是什么?)感谢您的快速回复。EventConsumer是在一个事件和一个操作之间配置的链。例如,EventConsumer可能包含执行提供的操作所需满足的进一步条件。可能的事件和操作是以编程方式定义的,但它们的连接可以从外部配置,例如,数据库谢谢你对PECS的提示,我修复了相关的位置。现在剩下的最后一个问题是在
Orchestrator
类中注册工厂。
registerFactory(EventConsumerFactory@user1801173这是一些你没有显示的代码。你能编辑你的问题以包括
registerFactory
方法和其他相关代码吗?我编辑了我的帖子,展示了ConsumerFactory和factory注册
  void registerConsumerFactory(ConsumerFactory<? super Event> factory) {
    this.consumerFactories.add(factory);
   }
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));
      }
    }
  }
}