3

我正在尋找一個好方法有時暫停一個動作(函數/方法調用),直到用戶確認他想要執行該動作的特定部分。我需要在不允許執行代碼的環境中執行此操作(本例中爲ActionScript,但JavaScript的方法應該相同)。提示用戶在進程中確認

爲了說明這一點,這是行動的實體模型引入用戶前提示:

<preliminary-phase> // this contains data needed by all the following phases // 

<mandatory-phase> // this will be always be executed // 

<optional-phase> // this will always execute too, if in this form, but in some cases we need to ask the user if he wants to do it // 

<ending-phase> // also mandatory // 



我需要的是插入一個有條件的用戶提示,一個「你想做這部分?「,並且只有在用戶需要時才做<optional-phase>

<preliminary-phase> 

<mandatory-phase> 

if(<user-confirmation-is-needed> and not <user-response-is-positive>){ 
    <do-nothing> 
} 
else{ 
    <optional-phase> 
} 

<ending-phase> 



當試圖做到這一點在ActionScript/JavaScript的我得到了這樣的事情:

<preliminary-phase> 

<mandatory-phase> 

if(<user-confirmation-is-needed>){ 
    askForConfirmation(callback = function(){ 
     if(<user-response-is-positive>) 
      <optional-phase> 
     <ending-phase> 
    }); 
    return; 
} 

<optional-phase> 

<ending-phase> 

現在無論<optional-phase><ending-phase>被複制。另外,因爲他們使用在<preliminary-phase>中創建的對象,我不能將所有數據傳遞到這些函數,而是將它們移動到外部函數。


我目前的解決方案是,我封閉每個<optional-phase><ending-phase>在某些局部功能(讓他們有機會獲得在<preliminary-phase>數據)宣佈之前,我要求確認我調用這些函數,而不是複製的代碼,但代碼不再以執行的順序看起來不正確。

你們會推薦什麼?

備註:
1. askForConfirmation是一個非阻塞函數。這意味着緊跟其調用的代碼會立即執行(這就是爲什麼我的方法中有一個return;)。

回答

3

注:我敢肯定,我知道你的確切情況不是100%。

命令模式可能適用於此。這與人們的建議類似。您有一組按順序執行的命令數組。

[<preliminary-phase>, <mandatory-phase>, <optional-phase>, <ending-phase>] 

只需將命令從陣列中移出一次,然後調用execute方法。

在可選階段,檢查是否需要用戶確認,如果不是,則執行可選代碼方法,該方法調度命令完成事件,如果需要,則顯示警報,等待事件,檢查結果並分派一個命令完成事件或調用可選方法(它將運行,然後分派命令完成)。

您也可以創建一個命令樹,這樣可以清楚地說明執行流程,而不必混淆數組。

這就是安裝嚮導程序的工作方式。

這很好,執行的順序很好,可見,你的代碼很好地分解成塊,每一步的複雜性被封裝。例如,可選階段對結束階段沒有任何瞭解。可選階段只知道用戶在執行之前可能需要提示,並且它在內部處理所有內容。

http://en.wikipedia.org/wiki/Command_pattern

「使用命令對象可以更容易地構建需要委託通用部件,序列或執行方法調用在自己選擇的時間......」

+0

命令模式的+1。我會研究它。 – 2010-11-10 11:30:36

1

「代碼不再按執行順序」實際上對我來說似乎很好。如果代碼沒有按照執行的順序編寫,只要它很清楚,就可以。事實上,由於你的代碼是以可變的順序執行的,我認爲你不可能按照執行的順序編寫它,而不需要重複代碼,這是一個更大的罪惡。選擇好的函數名稱,你的方法會通過我的代碼審查。

<preliminary-phase> 

<mandatory-phase> 

var optional_phase = function() { 
    <optional-phase> 
} 

var ending_phase = function() { 
    <ending-phase> 
} 

if(<user-confirmation-is-needed>){ 
    askForConfirmation(function(){ 
     if(<user-response-is-positive>) 
      optional_phase(); 
     ending_phase(); 
    }); 
    return; 
} 

optional_phase(); 
ending_phase(); 
+0

給出到目前爲止我的回答中的評論,我正在與這個答案。 – 2010-11-09 16:45:01

0

這是做你要求的嗎?

<preliminary-phase> 

<mandatory-phase> 

if(<user-confirmation-is-needed>){ 
    askForConfirmation(function(){ 
     if(<user-response-is-positive>) 
      <optional-phase-as-local-function> 
     <ending-phase-as-local-function> 
    }); 
} else { 
    <optional-phase-as-local-function> 
    <ending-phase-as-local-function> 
} 
+0

通過刪除'return'所做的操作是在''之前執行''。這絕對不行。請注意,主函數不會等到用戶選擇YES/NO時纔會執行完所有的代碼。此外,我正在尋找最佳做法。我已經在使用實用的解決方案。 – 2010-11-09 16:08:43

+0

如果'askForConfirmation'像javascript'confirm'命令一樣被阻塞,那麼這將起作用。然而,@Alin說,「我需要在不允許代碼執行停止的環境中執行此操作。」我認爲這意味着他的'askForConfirmation'不會阻止這意味着如果需要確認,您的解決方案將在'' – 2010-11-09 16:09:26

+0

之前執行'',對不起,我在「JavaScript」沒有意識到阻塞問題。當前編輯如何? – 2010-11-09 16:21:06

0

不是一個巨大的變化,但提供了這個流程的工作原理,可選階段,不重複

<preliminary-phase> 

<mandatory-phase> 

if(<user-confirmation-is-needed>){ 
    askForConfirmation(function(){ 
     if(<user-response-is-negative>) 
     { 
     <ending-phase> 
     return; 
     } 
    }); 
} 

<optional-phase> 
<ending-phase> 
+0

'askForConfirmation'是非阻塞的...我會編輯問題來說明這個更清楚。 – 2010-11-09 17:12:28

相關問題