枚举中的枚举 [英] Enum within an enum

查看:29
本文介绍了枚举中的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是我被卡住的问题,而是我正在寻找一种整洁的方式来编写我的代码.

This isn't a matter of me being stuck, but rather I'm looking for a tidy way to write my code.

本质上,我正在编写一个事件驱动的应用程序.用户触发事件,事件被发送到适当的对象,然后对象处理事件.现在我正在编写偶数处理程序方法,我希望使用 switch 语句来确定如何处理事件.现在,当我在处理一般结构时,事件类非常简单:

Essentially, I'm writing an event driven application. The user triggers an event, the event gets sent to the appropriate objects, and the objects handle the events. Now I'm working on writing the even handler methods, and I was hoping to use switch statements to determine how to handle the event. Right now whilst I'm working on the general structure, the event class is really simple:

public class Event {

    public static enum Action {
        MOVE, FOO, BAR
    }

    private Action action;
    private int duration;

    public Event(Action action, int duration) {
        this.action = action;
        this.duration = duration;
    }

    public Action getAction() {
        return action;
    }

    public int getDuration() {
        return duration;
    }

然后,在另一堂课上,我会有类似的东西:

Then, in another class, I'll have something like:

public void handleEvent(Event evt) {    
    switch(Event.getAction()) {
        case MOVE: doSomething(); break;
        case FOO:  doSomething(); break;
        case BAR:  doSomething(); break;
        default: break; 
    }
}

喜欢做的是这样的事情(尽管我当然会将 switch 语句粘贴到它们自己的函数中,以避免它变成令人讨厌的开关和案例毛球):

What I would like to do is something like this (though I would of course stick the switch statements into their own functions to avoid it turning into a nasty hairball of switches and cases):

public void handleEvent(Event evt) {    
    switch(Event.getAction()) {
        case MOVE: switch(Event.getAction()) {
                       case UP:    break;
                       case DOWN:  break;
                       case LEFT:  break;
                       case RIGHT: break;
                   }
        case FOO:  break;
        case BAR:  break;
        default:   break; 
    }
}

所以,我想创建嵌套的枚举......像这样:

So, I'd want to create nested enums... like so:

public static enum Action {
    public enum MOVE {UP, DOWN, LEFT, RIGHT}, FOO, BAR
}

我并不是无法避免这种情况,它只是……方便.因此,虽然上述实际上不起作用,但是否有一些类似的方法可以实现这一目标?如果我可以发送带有操作MOVE.UP"的事件,并且该方法首先将其识别为 MOVE 类型的操作,然后进一步识别它具体是在 UP 方向上,那就太好了.这只是一个简单的例子,如果我也能制作更长的链条就好了,比如DELETE.PAGE1.PARAGRAPH2.SENTENCE2.WORD11.LETTER3".在我看来,我将不得不使用字符串和大量 if/else 语句.希望有更好的方法!(哦,对我来说,性能很重要,如果有帮助的话)

It's not like I can't avoid the scenario, it would just be... convenient. So whilst the above doesn't actually work, is there some similar method to achieve this? It would be nice if I could send an event with the action "MOVE.UP", and the method would identify it first as an action of type MOVE, and then further identify that it is specifically in the UP direction. That's just a simple example, it would be grat if I could also make longer chains, something like "DELETE.PAGE1.PARAGRAPH2.SENTENCE2.WORD11.LETTER3". The way I see it, I'm just going to have to use Strings and lots of if/else statements. Hoping there's a better way! (Oh, and performance matters in my case, if that helps)

推荐答案

也许对事件使用继承层次结构?

Perhaps use an inheritance hierarchy for the Events?

所以你有:

- abstract Event
-- MoveEvent(Direction)
-- FooEvent()
-- BarEvent()

可能更有意义:

- abstract Event
-- abstract MoveEvent
--- MoveUpEvent
--- MoveDownEvent
--- MoveRightEvent
--- MoveLeftEvent
-- FooEvent
-- BarEvent

如果所有的 Move 事件都有一个距离,那么将它传递给 MoveEvent 构造函数(它会向下波动).

If all the Move events have a distance, then pass that into the MoveEvent constructor (which will ripple down).

这篇关于枚举中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆