我应该使用什么模式在Node/JavaScript中创建侦听机器人?

我应该使用什么模式在Node/JavaScript中创建侦听机器人?,javascript,node.js,design-patterns,Javascript,Node.js,Design Patterns,我目前正在使用Node来制作我的第一个机器人,但我有点困惑如何才能将其变成现实。问题是,对于这类东西,我应该使用什么样的最佳模式和模式名称? 基本上,一个人可以听一个主题和演讲者 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 du

我目前正在使用Node来制作我的第一个机器人,但我有点困惑如何才能将其变成现实。问题是,对于这类东西,我应该使用什么样的最佳模式和模式名称?

基本上,一个人可以听一个主题和演讲者

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!"
  }
}

很抱歉,没有完成代码,但这是我能做的最远的事。

如果你在谈论对话逻辑,责任链似乎是一条路要走。也就是说,这只适用于实现响应树。你可能有不同的需求

可行的方法是: 逻辑树中的所有节点都将使用此方法

bool consider(utterence, history);
如果节点或其子节点之一已处理该情况,则此方法将返回true

我将实现几个主题检测器和一个通用的“我想不出一个好的答案”,以及一群专门的响应者

逻辑应该是这样的:

(天气探测器考虑话语、历史) (确定主题为天气) (传递到处理天气对话的对象链)

(天气探测器考虑话语、历史) (确定主题不是天气) (传递到主题检测器链中的下一个) (genericFunnyResponder考虑话语、历史)
(随机选择一个古怪的答案)

可以发出并监听事件。真正简单的API: