2012-06-28 19 views
3

我目前正在使用Node來製作我的第一個機器人,但我對於如何將其變爲現實感到有點困惑。問題是,什麼是我應該使用這種東西的模式的最佳模式和名稱?Node/JavaScript中應該使用什麼模式來製作監聽器?

基本上,一個人可以聽一個主題和說話人。

var test = person("ask_name","hallo person you are special"); 
console.log(test); // should return thanks! 

var test = person("ask_name","hallo person you are dumb as the bird"); 
console.log(test); // should return i hate you! 

function person(ability, body) { 

    console.log(ability,body); 
    var ability_name = "ability_" + ability; 
    console.log(ability_name,typeof ability_name); // ignore all of this, trying something 
    if (typeof ability_name) {}; 

    // ability list array 
    var ability = []; 

    // Search for ability 
    // not done 

    var say_bad = function() { 
    this.listen_subject = 'ask_name'; 
    this.listen_body = 'you are dumb'; 
    return "i hate you!" 
    } 

    var say_good = function() { 
    this.listen_subject = 'ask_name'; 
    this.listen_body = 'you are special'; 

    return "thanks!" 
    } 
} 

對不起,沒有完成代碼,但這是我可以走的最遠的地方。

+0

可以發射和偵聽事件。真正簡單的API:http://nodejs.org/api/events.html – danneu

回答

0

如果您正在談論對話邏輯,責任鏈看起來就像是要走的路。這就是說,這隻適用於實現響應樹。你可能有不同的需求。

可行的方法是: 邏輯樹中的所有節點都有這個方法;

bool consider(utterence, history); 

如果節點或其某個子節點處理了這種情況,則此方法將返回true。

我會實現幾個主題探測器和一個通用的「我不能拿出一個很好的答案」,以及一堆專門的響應者。

邏輯會是這樣的:

(weatherDetector認爲utterence,歷史) (確定主題是天氣) (上傳遞給鏈中處理天氣對話對象)

(確定話題不是天氣) (傳遞給主題檢測器鏈中的下一個) (genericFunnyResponder考慮(挑選,歷史) (隨機選擇一個古怪的答案)

相關問題