1

here's a programming style question about the best strategy to map input keys to actions in a class that implement the state pattern.

I'm dealing with two classes:

The first implements the state pattern, which controls a multi-state physical device:

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() { ... }
};

The second is a KeyListener that retrieves all keyboard input and calls the appropriate action from the device controller when a pressed input key matches a (for the time being) hard-coded bindings table:

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' :
        ...
    }
    ...
}

Is there a best-practice coding style to bind the keys to the actions in the controller ? Do I have to go through a switch statement, as in the sample code ? It seems to me that this solution is somewhat dirty code: isn't the state pattern supposed to eliminate unmaintanable if and switch control structures ?

Thank you for your suggenstions.

4

1 に答える 1

0

ポリモーフィズムを使用すると、目標を達成できます。私は列挙型を使用しましたが、インターフェイスまたは抽象クラスを使用してから、主要なプロセッサのそれぞれを実装する方が適切かもしれません。どう思いますか?

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 {
}
于 2012-05-18T10:43:22.923 に答える