存储或计算复杂模型的总计

时间:2010-10-18 10:22:35

标签: oop zend-framework frameworks data-modeling

所有

我正在使用Zend Framework开发一个应用程序来管理建筑工程的招标。

招标将是一组相当复杂的模型,其架构类似于下面的代码片段。

我的问题是......我应该将投标的总价值作为投标模型的一部分存储,还是应该在每次需要时计算?招标的总价值将是所有组成部分的总和(例如工厂/人工/材料/间接费用等)。

object(Application_Model_Tender)#75 (4) {
  ["_id":protected] => int(1)
  ["_name":protected] => string(33) "Tender Name"
  ["_due":protected] => string(10) "2010-12-01"
  ["_labour":protected] => array(2) {
    [0] => object(Application_Model_Gang)#81 (3) {
      ["_id":protected] => int(1)
      ["_name":protected] => string(25) "First Gang Name"
      ["_gangMembers":protected] => array(2) {
        [0] => object(Application_Model_GangMember)#93 (5) {
          ["_id":protected] => NULL
          ["_name":protected] => string(11) "Labour Type"
          ["_workingPattern":protected] => string(7) "Default"
          ["_cost":protected] => float(546)
          ["_qty":protected] => int(3)
        }
        [1] => object(Application_Model_GangMember)#91 (5) {
          ["_id":protected] => NULL
          ["_name":protected] => string(11) "Labour Type"
          ["_workingPattern":protected] => string(8) "Custom 1"
          ["_cost":protected] => float(777)
          ["_qty":protected] => int(1)
        }
      }
    }
    [1] => object(Application_Model_Gang)#90 (3) {
      ["_id":protected] => int(2)
      ["_name":protected] => string(15) "Second Gang Name"
      ["_gangMembers":protected] => array(1) {
        [0] => object(Application_Model_GangMember)#92 (5) {
          ["_id":protected] => NULL
          ["_name":protected] => string(11) "Labour Type"
          ["_workingPattern":protected] => string(8) "Custom 1"
          ["_cost":protected] => float(777)
          ["_qty":protected] => int(2)
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我不知道具体是什么,但作为一般规则,我建议将计算作为初始策略。仅在出于性能原因需要时才存储它。理由:

  1. 您需要在两种情况下都编写计算功能。
  2. 如果你存储(缓存)它,你需要一个策略来检测招标结构的变化。更新缓存的值。这是额外的复杂性,因此只有在您发现需要它时才会引入它。
  3. 您应该能够隐藏接口[getTotal()]背后的实现策略 - 因此,如果您决定在以后缓存,则不必更改调用客户端。

    (上面当然有一些警告。如果结构是不可变的,那么你可以安全地缓存而无需更新检测)。