Class 处理对象中计算属性的正确方法是什么?

Class 处理对象中计算属性的正确方法是什么?,class,oop,methods,Class,Oop,Methods,据我所知,实现这一目标的主要方法有两种: 选项1:通过getter方法“按需”计算每个属性(例如。 getTotal()) 选项2:在构建时以及公共属性随时更改时,使用 通用的calculate()方法 我使用一个简单的保险单对象创建了两个示例。每个类都使用一些属性进行初始化,如premiumRateBase、brokerFee、termYears、effectiveDate和agentCommissionRate。计算出的列将是按比例分配的premiumRate、total或agentCo

据我所知,实现这一目标的主要方法有两种:

  • 选项1:通过getter方法“按需”计算每个属性(例如。
    getTotal()
  • 选项2:在构建时以及公共属性随时更改时,使用 通用的
    calculate()
    方法
我使用一个简单的保险单对象创建了两个示例。每个类都使用一些属性进行初始化,如
premiumRateBase
brokerFee
termYears
effectiveDate
agentCommissionRate
。计算出的列将是按比例分配的
premiumRate
total
agentCommission

以下是选项1的示例:

component {

    // public properties (getters + setters)
    property name="premiumRateBase";
    property name="brokerFee";
    property name="effectiveDate";
    property name="termYears";
    property name="agentCommissionRate";

    function init(
        required numeric premiumRateBase,
        required numeric brokerFee,
        required date effectiveDate,
        required numeric termYears,
        required numeric agentCommissionRate
    ) {

        // setters
        ...

        return this;

    }

    function getExpirationDate() {
        return dateAdd( 'yyyy', effectiveDate, termYears );
    }


    function getPremiumRate() {

        // run proration and calcuation determination 
        // based on premiumRateBase, getExpirationDate(), and maybe a few other methods
        ...

        return premiumRate;

    }


    function getTotal() {
        return getPremiumRate() + brokerFee;
    }


    function getAgentCommission() {
        return getPremiumRate() * agentCommissionRate
    }


    function getAgentCompensation() {
        return getAgentCommission() + brokerFee
    }

}
在上面的示例中,只要调用像
getTotal()
这样的方法,就会运行计算。这种方法的优点是代码非常简单。这种方法的缺点是,如果需要运行
getTotal()
getAgentCommission()
,然后再运行
getagentcompension()
,则会运行大量冗余的数学运算。在这个例子中,这并不意味着额外的处理时间,但在一个更复杂的例子中,我可以看到这一点

下面是选项2的一个示例:

component {

    // public properties (getters + setters)
    property name="premiumRateBase";
    property name="brokerFee";
    property name="effectiveDate";
    property name="termYears";
    property name="agentCommissionRate";


    function init(
        required numeric premiumRateBase,
        required numeric brokerFee,
        required date effectiveDate,
        required numeric termYears,
        required numeric agentCommissionRate
    ) {

        // setters
        ...

        // run the calculation
        calculate();

        return this;

    }


    // primary calculation method which sets all private properties
    function calculate() {

        variables.expirationDate = calculateExpirationDate();
        variables.premiumRate = calculatePremiumRate();
        variables.total = calculateTotal();
        variables.agentCommission = calculateAgentCommission();

    }


    /***************************
        Public Getters
    ***************************/

    function getExpirationDate() {
        return expirationDate;
    }

    function getPremiumRate() {
        return premiumRate;
    }

    function getTotal() {
        return total;
    }

    function getAgentCommission() {
        return agentCommission;
    }


    /***************************
        Private Calculations
    ***************************/

    private function calculateExpirationDate() {
        return dateAdd( 'yyyy', effectiveDate, termYears );
    }


    private function calculatePremiumRate() {

        // run proration and calcuation determination 
        // based on premiumRateBase, expirationDate and maybe a few other variables
        ...

        return premiumRate;

    }


    private function calculateTotal() {
        return premiumRate + brokerFee;
    }


    private function calculateAgentCommission() {
        return premiumRate * agentCommissionRate;
    }


    private function calculateAgentCompensation() {
        return agentCommission + brokerFee;
    }


}
在第二个示例中,我们仅在构造函数方法
init()
激发之后运行泛型
calculate()
方法。我没有包括这个,但是如果您曾经通过setter方法更新任何公共属性,您还需要再次运行
calculate()
。这种方法的优点是,计算数学仅在属性更改时发生。缺点是代码似乎有点复杂,更难阅读


解决这类问题的最佳做法或适当方法是什么?

这是一个常见的难题,最终解决的是每次重新计算财产的成本。如果价格便宜,我总是更喜欢getter方法

另一个需要考虑的方面是计算属性的陈旧性。选项#2仅在初始化时执行计算,计算中涉及的属性可能会在初始化后更改。计算的度量现在将过时。您可以通过在修改时重新计算度量来修复它。这将进一步增加代码的复杂性。如果这个修改没有很好地封装,重新计算的责任也将与调用方共享

总之,对于便宜的计算,我更喜欢选项#1,对于复杂的计算,我会首先封装修改,以确保每次更新时重新计算