2013-06-20 32 views
1

我期待在一個項目中實現一種異步狀態機,並且我正在尋找一種方法在控制器中存儲一個當它準備好時執行的方法列表。有沒有在J2ME中使用類似getClass.getMethod的方法?

你們知道一種方法嗎?

一位同事想到使用一個接口,我們將實現內聯並將相關代碼放入對象的已實現方法中,但我想知道它是否可以以更簡單的方式實現。

在此先感謝您的答案。

+0

在Java中,函數不是一等公民,所以每當你需要把一個函數作爲一個對象,你需要一個封裝對象(又名[命令模式](http://en.wikipedia.org/wiki/Command_pattern),就像'Runnable')。 –

回答

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


} 
相關問題