【问题标题】:Applying key bindings to state transitions when implementing a state pattern在实现状态模式时将键绑定应用于状态转换
【发布时间】:2011-07-21 21:56:38
【问题描述】:

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

我正在处理两个类:

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

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 控制结构吗?

感谢您的建议。

【问题讨论】:

    标签: java key-bindings state-pattern


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 2023-03-27
      • 2014-04-06
      • 1970-01-01
      相关资源
      最近更新 更多