javascript - 我应该如何处理多个依赖于订单的条件?

标签 javascript performance readability

我有多个并发条件,其中顺序取决于条件。我已经想出了多种方法来处理这个问题,我需要帮助从我准备的几种解决方案中选择最佳的解决方案。需要考虑的事情是:性能、可重用性、可读性、内存消耗。

方法一:定义多个嵌套的 switch case

var collision = {
    detect1: function(subject, target){
        // multiple switch cases
        var shapes = {};
        shapes[subject.type] = subject;
        shapes[target.type] = target;

        switch(subject.type) {
            case 'rectangle':
                switch(target.type) {
                    case 'ellipse':
                        return this.rectWithEllipse(subject, target);
                }
                break;
            case 'ellipse':
                switch(target.type) {
                    case 'rectangle':
                        return this.rectWithEllipse(target, subject);
                }
        }
    },

方法 2:将类型存储在对象注册表中,并根据参数测试切换顺序

    detect2: function(subject, target){
        // object registry and place switch
        var shapes = {};
        shapes[subject.type] = subject;
        shapes[target.type] = target;

        var shape1 = subject;
        var shape2 = target;

        var reverseShapeOrder = function() {
            shape2 = target;
            shape1 = subject;
        };

        if ( shapes.rectangle && shapes.ellipse ) {
            if (subject.type === 'ellipse') {
                reverseShapeOrder();
                return this.rectWithEllipse(shape1, shape2);
            }
        }
    },

方法 3:将类型连接到字符串并根据 indexOf 测试顺序切换顺序。

    detect3: function(subject, target) {
        // string concat and decoding with place switch
        var shapeString = subject.type + target.type;

        var rectIndex =  shapeString.indexOf('rectangle');
        var ellipseIndex = shapeString.indexOf('ellipse');
        var pointIndex = shapeString.indexOf('point');

        var shape1 = subject;
        var shape2 = target;

        var reverseShapeOrder = function() {
            shape2 = target;
            shape1 = subject;
        };

        if (rectIndex && ellipseIndex) {
            if (ellipseIndex < rectIndex) {
                reverseShapeOrder();
            }
            return this.rectWithEllipse(shape1, shape2);
        }
    },

方法 4:标准传统 if-else 语句

    // traditional logic
    detect4: function(subject, target) {
        if (subject.type === 'rectangle'  && target.type === 'ellipse') {
            return this.rectWithEllipse(subject, target);
        }
        else if (subject.type ==='ellipse' && target.type === 'rectangle') {
            return this.rectWithEllipse(target, subject);
        }
    },
    rectWithEllipse: function(rect, ellipse) {
        return false;
    }
};   

方法 5:带有引用函数的动态选择器(感谢@Bergi 的动态选择器建议)

detect5: function(subject, target) {
        return this[subject.type + '_with_' + target.type](subject, target);
    },
    rect_with_ellipse: function(rect, ellipse) {
        return false;
    },
    ellipse_with_rect: function(rect, ellipse) {
        this.rect_with_ellipse(ellipse, rect);
    }
};   

请帮助我选择最佳解决方案并了解为什么它是最好的。谢谢

请记住,完整的组合列表会更大,如下所示:

rectWithPoint: function(rect, point) {
    return false;
},
rectWithEllipse: function(rect, ellipse) {
    return false;
},
rectWithRect: function(rect, rect) {
    return false;
},
ellipseWithPoint: function(ellipse, point) {
    return false;
},
ellipseWithEllipse: function(ellipse, ellipse) {
    return false;
}

最佳答案

我将定义一个映射如下,这将使其易于扩展:

targets: {
  rectangle: {
    point: function rectWithPoint(rect, point) {
      return false;
    },
    ellipse: function rectWithEllipse(rect, ellipse) {
      return false;
    },
    rectangle: function rectWithRect(rectLhs, rectRhs) {
      return false;
    }
  },
  ellipse: {
    point: function ellipseWithPoint(ellipse, point) {
      return false;
    },
    ellipse: function ellipseWithEllipse(ellipseLhs, ellipseRhs) {
      return false;
    }
  }
},

detect5: function (subject, target) {
  var tmp, candidates = this.targets[subject];

  if (!candidates) {
    tmp = subject;
    subject = target;
    target = tmp;
    candidates = this.targets[subject];
  }

  if (candidates && candidates.hasOwnProperty(target)) {
    return candidates[target].call(this, subject, target);
  }

  return false;
}

关于javascript - 我应该如何处理多个依赖于订单的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38134773/

相关文章:

java - 如何监控 Java 进程的性能?

php - 省略 PHP 三元和空值合并运算符中的 'else'

javascript - 在 Angular-bootstrap 弹出窗口中使用指令/隔离范围作为 HTML

c - size_t 和 sizeof 之间的差异

javascript - 从js引擎的 Angular 看闭包和构造函数是如何工作的

performance - 在 Haskell 中优化 n-queens

c# - 包装字典以提高可读性?

c# - 使用更少代码的成员方法转换/解析许多对象

javascript - 了解基本的 JavaScript 逻辑

javascript - 在 jpgraph js 库中,对于折线图,是否可以有不同(自定义)颜色的点?