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

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

问题描述

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



本质上,我在写事件驱动的应用程序。用户触发事件,将事件发送到适当的对象,并且对象处理事件。现在我正在编写甚至处理程序方法,我希望使用switch语句来确定如何处理事件。现在,当我正在开发一般结构时,事件类非常简单:

  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;
}

然后,在另一个类中,我会有如下的东西:

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

我会像要做的就是这样的(虽然我当然会把switch语句放在自己的函数中,以避免它变成一个讨厌的头发的开关和案例):

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

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

  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语句。希望有更好的方法! (哦,性能在我的情况下,如果这有帮助)

解决方案

也许使用事件的继承层次结构? p>

所以你有:

   - 抽象事件
- MoveEvent(Direction)
- FooEvent()
- BarEvent()

有可能更有意义:

   - 抽象事件
- 抽象MoveEvent
- - MoveUpEvent
--- MoveDownEvent
--- MoveRightEvent
--- MoveLeftEvent
- FooEvent
- BarEvent

如果所有的Move事件都有一个距离,然后将其传递到MoveEvent构造函数(将会下降)。


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

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; 
    }
}

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
}

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?

So you have:

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

It may make more sense to have:

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

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

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

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