Java 在实现状态模式时将键绑定应用于状态转换

Java 在实现状态模式时将键绑定应用于状态转换,java,key-bindings,state-pattern,Java,Key Bindings,State Pattern,这里有一个编程风格的问题,关于将输入键映射到实现状态模式的类中的操作的最佳策略 我要处理两个类: 第一个实现状态模式,该模式控制多状态物理设备: class DeviceController { State _a, _b, _current; // Actions that may prompt a transition from one state to another public void actionA() { ... } public void act

这里有一个编程风格的问题,关于将输入键映射到实现状态模式的类中的操作的最佳策略

我要处理两个类:

第一个实现状态模式,该模式控制多状态物理设备:

class DeviceController {
    State _a, _b, _current;

    // Actions that may prompt a transition from one state to another
    public void actionA() { ... }
    public void actionB() { ... }
    public void actionC() { ... }

    public State getStateA() { ... }
    public State getStateB() { ... }

    public void setCurrentState() { ... }
};
第二个是KeyListener,当按下的输入键与(目前)硬编码绑定表匹配时,它检索所有键盘输入并从设备控制器调用适当的操作:

class KeyDemo implements KeyListener {

    DeviceController _controller;
    ...
    @Override
    public void keyPressed(KeyEvent arg0) {
        char key = Character.toUpperCase(arg0.getKeyChar());
        switch (key) {
        case 'A':
            _controller.actionA();
            break;
        case 'B' :
        ...
    }
    ...
}
是否有将键绑定到控制器中的操作的最佳实践编码样式?我是否必须像在示例代码中那样执行switch语句?在我看来,这个解决方案有点脏代码:状态模式不是应该消除不可维护的if和switch控制结构吗


感谢您的建议。

使用多态性可以实现您的目标。我曾经使用过enum,但可能更适合使用接口或抽象类,然后实现每个关键处理器。你觉得怎么样

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

enum KeyProcessor {
    A {
        void executeAction() {
            _controller.actionA();
        }
    },
    B {
        void executeAction() {
            _controller.actionB();
        }
    };

    private static final DeviceController _controller = new DeviceController();

    void executeAction() {
        System.out.println("no action defined");
    }
}

class DeviceController {
    State _a;
    State _b;
    State _current;

    // Actions that may prompt a transition from one state to another
    public void actionA() {
        System.out.println("action A performed.");

    }

    public void actionB() {
        System.out.println("action B performed.");
    }

    public void actionC() {
    }

    public State getStateA() {
        return null;
    }

    public State getStateB() {
        return null;
    }

    public void setCurrentState() {
    }
} // end class DeviceController

public class KeyDemo implements KeyListener {

    DeviceController _controller;

    // ...
    @Override
    public void keyPressed(KeyEvent arg0) {
        keyPressed(Character.toUpperCase(arg0.getKeyChar()));
        // ...
    }

    public void keyPressed(char c) {
        KeyProcessor processor = KeyProcessor.valueOf(c + "");
        if (processor != null) {
        processor.executeAction();
    }

    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    public static final void main(String[] args) {
        KeyDemo main = new KeyDemo();
        main.keyPressed('A');
        main.keyPressed('B');
    }
} // end class KeyDemo

class State {
}