1

プロジェクトに一種の非同期ステート マシンを実装しようとしています。その一部として、準備が整ったときに実行するメソッドのリストをコントローラーに格納する方法を探していました。

あなたはそれを行う方法を知っていますか?

同僚は、インラインで実装し、関連するコードをオブジェクトの実装メソッドに配置するインターフェイスを使用することを考えていましたが、もっと簡単な方法で実行できるかどうか疑問に思っていました。

ご回答ありがとうございます。

4

1 に答える 1

0

最終的に行ったことは次のとおりです。

    // /////////////////////////////////
// STATE MACHINE SECTION //
// /////////////////////////////////

    /**
     * State abstract class to use with the state machine
     */
    private abstract class State {

        private ApplicationController applicationController;

        public State() {}

        public State(ApplicationController ac) {
            this.applicationController = ac;

        }

        public abstract void execute();

        public ApplicationController getApplicationController() {
            return applicationController;
        }


    }

    /**
     * The next states to execute.
     */
    private Vector nextStates; //Initialized in the constructor

    private boolean loopRunning = false;

    /**
     * Start the loop that will State.execute until there are no further 
     * step in the current flow.
     */
    public void startLoop() {

        State currentState;
        loopRunning = true;

        while(!nextStates.isEmpty()) {
            currentState = (State) nextStates.firstElement();
            nextStates.removeElement(currentState);
            currentState.execute();
        }

        loopRunning = false;
    } 


    /**
     * Set the next state to execute and start the loop if it isn't running.
     * @param nextState 
     */
    private void setNextState(State nextState) {
        this.nextStates.addElement(nextState);
        if(loopRunning == false)
            startLoop();
    }


public void onCallbackFromOtherSubSystem() {
        setNextState(new State() {

            public void execute() {
                try {
                        functionTOExecute();
                } catch (Exception e) {
                        logger.f(01, "Exception - ", errorDetails, e);
                }   
            }
        });


}
于 2013-06-25T15:38:19.273 に答える