通用接口扩展Enums的通用

时间:2014-02-20 13:08:12

标签: java generics event-handling

我正在尝试编写一个通用的事件系统。为此,我想为这样的EventHandler创建一个接口*(这不起作用)*:

public interface GameEventHandler<I extends GameEvent<TYPE extends Enum<?>, ATT extends Enum<?>>> {
    public void handleEvent(final GameEvent<TYPE, ATT>... e);
    public void registerListener(final GameEventListener<I> listener,
            final TYPE... type);
    public void unregisterListener(final GameEventListener<I>... listener);
    public void unregisterAllListener();
    public void unregisterAllListener(final I... type);
    public void processEvents();
    public void processEvents(final int maxTimeInMS);
}

但这不起作用,因为我“想”它。

事件本身是一个通用元素,它非常简单:

public class GameEvent<T extends Enum<?>, S extends Enum<?>> {
    private HashMap<S, String> values;
    private T type;

    public void init(T type) {
        this.type = type;
    }

    public T getType() {
        return this.type;
    }

    public void addMessage(S t, String value) {
        this.values.put(t, value);
    }

    public void getMessage(S t) {
        this.values.get(t);
    }
}

如果我实现HandlerInterface id就像让它仍然是通用的那样,有一些像DefaultHandler<GameEvent<TypeEnum, AttributEnum>>()来启动它。因此,您可以使用界面创建自己的处理程序或使用我提供的DefaultHandler,但仍可以使用您自己的Enums

目前我能够创建如下界面:

public interface GameEventHandler<I extends GameEvent<TYPE, ATT>, TYPE extends Enum<?>, ATT extends Enum<?>> 

但我没有得到DefaultHandler Generic的实现

public class DefaultGameEventHandler implements GameEventHandler<GameEvent<EventTypes, AttributeTypes>, EventTypes, AttributeTypes>  // not generic those are testing Enums

那我该怎么办?是否有可能像我想拥有它一样?

2 个答案:

答案 0 :(得分:1)

说实话,我不确定我是否完全理解你的要求。但是如果你想让DefaultGameEventHandler尽可能通用,我建议采用以下方法:

使GameEventHandler接口接受一个EventType接口和一个AttributeType接口,而不是让一个接受Enums作为通用EventTypes和AttributeTypes的GameEventHandler接口:

public interface GameEventHandler<I extends GameEvent<TYPE, ATT>, TYPE extends EventType, ATT extends  AttributeType> {

    public void handleEvent(final GameEvent<TYPE, ATT>... e);
    public void registerListener(final GameEventListener<I> listener, final TYPE... type);

    public void unregisterListener(final GameEventListener<I>... listener);

    public void unregisterAllListener();
    public void unregisterAllListener(final I... type);

    public void processEvents();
    public void processEvents(final int maxTimeInMS);

}

以下是相应的接口和GameEvent类:

public interface EventType {
    // common functionality of EventTypes, if any
}

public interface AttributeType {
    // common functionality of AttributeTypes , if any
}

public class GameEvent<T extends EventType, S extends AttributeType> {
    private HashMap<S, String> values;
    private T type;

    public void init(T type) {
        this.type = type;
    }

    public T getType() {
        return this.type;
    }

    public void addMessage(S t, String value) {
        this.values.put(t, value);
    }

    public void getMessage(S t) {
        this.values.get(t);
    }
}

然后,您可以创建任意数量的实现这些接口的枚举:

enum MyEventTypes implements EventType{TYPE_1,TYPE_2,TYPE_3}

enum MyAttributeTypes implements AttributeType{ATT_1,ATT_2,ATT_3}

如果您绝对需要接口来提供Enum类的功能,您仍然可以在界面中指定它,如下所示:

public interface EventType {
    Enum<?> asEnum();
}

enum MyEventTypes implements EventType{
    TYPE_1,TYPE_2,TYPE_3;
    @Override
    public Enum<?> asEnum() {return this;}
}

现在您可以创建一个实现GameEventHandler接口的通用DefaultGameEventHandler类:

public class DefaultGameEventHandler<I extends GameEvent<TYPE, ATT>, TYPE extends EventType, ATT extends  AttributeType> implements GameEventHandler<I, TYPE, ATT>{ 

    @Override
    public void handleEvent(GameEvent<TYPE, ATT>... e) {
        //...
    }
    @Override
    public void registerListener(GameEventListener<I> listener, TYPE... type) {
        //...
    }
    @Override
    public void unregisterListener(GameEventListener<I>... listener) {
        //...
    }
    @Override
    public void unregisterAllListener() {
        //...
    }
    @Override
    public void unregisterAllListener(I... type) {
        //...
    }
    @Override
    public void processEvents() {
        //...
    }
    @Override
    public void processEvents(int maxTimeInMS) {
        //...
    }
}

您可以使用枚举

实例化DefaultGameEventHandler
//MyEventTypes and MyAttributeTypes are enums implementing EventType respectively AttributeType
DefaultGameEventHandler<GameEvent<MyEventTypes, MyAttributeTypes>, MyEventTypes, MyAttributeTypes> handler = new DefaultGameEventHandler<>();
GameEvent<MyEventTypes, MyAttributeTypes> event = new GameEvent<>();
event.addMessage(MyAttributeTypes.ATT_1, "some Message");
event.init(MyEventTypes.TYPE_1);
handler.handleEvent(event);
switch (event.getType()) {
    case TYPE_1:
        System.out.println("TYPE_1");
        break;
    case TYPE_2:
        System.out.println("TYPE_2");
        break;
    case TYPE_3:
        System.out.println("TYPE_3");
        break;
    default:
        break;
}

或者还有接口或实现接口的任何类:

DefaultGameEventHandler<GameEvent<EventType, AttributeType>, EventType, AttributeType> handler = new DefaultGameEventHandler<>();
GameEvent<EventType, AttributeType> event = new GameEvent<>();
event.addMessage(MyAttributeTypes.ATT_1, "some Message");
event.init(MyEventTypes.TYPE_1);
handler.handleEvent(event);
EventType type = event.getType();
// To switch on the type you could use the asEnum() method
// and cast the type to the corresponding enum if possible:
if (type.asEnum().getClass() == MyEventTypes.class) {
    MyEventTypes t = (MyEventTypes)type.asEnum();
    switch (t) {
        case TYPE_1:
            System.out.println("TYPE_1");
            break;
        case TYPE_2:
            System.out.println("TYPE_2");
            break;
        case TYPE_3:
            System.out.println("TYPE_3");
            break;
        default:
            break;
    }
}
// Or you could also directly switch on the name of the enum (not recommended!):
switch (type.asEnum().name()) {
    case "TYPE_1":
        System.out.println("TYPE_1");
        break;
    case "TYPE_2":
        System.out.println("TYPE_2");
        break;
    case "TYPE_3":
        System.out.println("TYPE_3");
        break;
    default:
        break;
}

编辑 - 回复BennX的评论:

  

我认为,定义Eventhandlers会更好   和2个枚举的GameEvents。但我认为它不可能只是   已标记的枚举。

实际上我不想表明,使用Enums是不可能的。如果您愿意,可以使用Enums完全替换我的示例中的接口:

public class GameEvent<T extends Enum<?>, S extends Enum<?>>

public interface GameEventHandler<I extends GameEvent<TYPE, ATT>, TYPE extends Enum<?>, ATT extends  Enum<?>>

public class DefaultGameEventHandler<I extends GameEvent<TYPE, ATT>, TYPE extends Enum<?>, ATT extends  Enum<?>> implements GameEventHandler<I, TYPE, ATT>

但是为什么强迫Enums对GameEventHandler的最终用户?如果您在EventType和AttributeType中不需要任何常用功能,那么您甚至可以不使用任何枚举或接口,并使GameEventHandler完全通用:

public interface GameEventHandler<I extends GameEvent<TYPE, ATT>, TYPE, ATT>

public class GameEvent<T, S>

public class DefaultGameEventHandler<I extends GameEvent<TYPE, ATT>, TYPE, ATT> implements GameEventHandler<I, TYPE, ATT>

我上面发布的示例代码,其中一个实例化带有枚举的DefaultGameEventHandler,仍将使用此通用GameEventHandler。但是,除了枚举之外,用户还可以决定使用最终的整数,最终字符串或任何其他对象作为事件和属性类型。

答案 1 :(得分:-1)

我认为你必须使用DefaultGameEventHandler之类的:

class DefaultGameEventHandler<TYPE extends Enum<?>,ATT extends Enum<?>>  implements GameEventHandler<GameEvent<TYPE, ATT>,TYPE,ATT>