从Java的构造函数内部调用私有方法

时间:2018-07-31 13:02:19

标签: java design-patterns

我有以下课程:

package com.tesco.demandforecasting.group8.choprachap7;

import java.util.ArrayList;

import com.tesco.demandforecasting.group8.utils.MathOperUtils;
import com.tesco.demandforecasting.group8.utils.RegressionUtils;

import lombok.Getter;

/**
 * This class if used to find seasonality factor of each period, as explain in
 * the chapter See https://kelley.iu.edu/mabert/e730/Chopra-Chap-7.pdf for the
 * explanation
 */
@Getter
public class ChopraChap7SeasonalFactorsCalculator {

    private double[] regressionParams;

    private int sales_size;
    private int periodicity;

    private ArrayList<Integer> sales;
    private ArrayList<Double> deseasonalisedData;
    private ArrayList<Double> deseasonalisedDemandUsingRegression;
    private ArrayList<Double> seasonalityFactors;

    public ChopraChap7SeasonalFactorsCalculator() {

        this.sales = new ArrayList<>();
        this.deseasonalisedData = new ArrayList<>();
        this.deseasonalisedDemandUsingRegression = new ArrayList<>();
        this.seasonalityFactors = new ArrayList<>();

        this.sales.add(8000);
        this.sales.add(13000);
        this.sales.add(23000);
        this.sales.add(34000);
        this.sales.add(10000);
        this.sales.add(18000);
        this.sales.add(23000);
        this.sales.add(38000);
        this.sales.add(12000);
        this.sales.add(13000);
        this.sales.add(32000);
        this.sales.add(41000);

        this.sales_size = sales.size();
        this.periodicity = 4;

        calculateSeasonalityFactors();
    }


    private void calculateSeasonalityFactors() {

        .......
        .......

        this.seasonalityFactors = seasonalityFactors;
        this.deseasonalisedDemandUsingRegression = deseasonalisedDemandUsingRegression;
        this.deseasonalisedData = deseasonalisedData;

    }

}

我想使用各自的getter将类字段公开给外部类。但是,问题在于,只有在调用 ChopraChap7SeasonalFactorsCalculator()方法之后,这些字段才能获得任何值。因此,我在这里所做的就是在创建类的对象后立即调用该方法。当然可以,但这是一种好的设计模式吗?

假设我不会从构造函数中调用该方法。因此,如果我们下面的代码是其他类:

ChopraChap7SeasonalFactorsCalculator calc = new ChopraChap7SeasonalFactorsCalculator();
calc.getDeseasonalisedData();

这将返回任何空数组列表。如何确保在访问任何字段之前调用该方法?

在我看来,最好的设计模式是什么?

2 个答案:

答案 0 :(得分:2)

  

当然可以,但这是一种好的设计模式吗?

这是一个非常正确的设计。您可以将构造函数逻辑的一部分委托给私有方法,以使事情更清楚。

  

这将返回任何空数组列表。我如何确保   在访问任何字段之前调用方法?

您担心有人会在构造函数中进行某些更改可能适用于任何方法或代码块。
但是,应用程序并非旨在检查每个组件是否达到了我们的期望。这是单元测试角色,用于断言实际行为是预期的行为。

因此,为$a_l_demographics = [11,3,13]; $a_p_demographics = ['a','b','c']; $a_r_demographics = []; $c_demographics_values = array(); $a_c_demographics = min(count($a_l_demographics), count($a_p_demographics)); for ($i = 0; $i < $a_c_demographics; $i++) { for ($j = 1; $j <= $a_l_demographics[$i]; $j++) { $a_r_demographics[] = $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT); $implode_demographics = implode($a_r_demographics, ','); // this won't work echo $implode_demographics; } } // this works // echo $implode_demographics; 构造函数编写单元测试,并在此测试中断言,一旦创建对象,所有吸气剂都将返回期望值。
如果有人以错误的方式修改了构造函数,则测试将失败,构建也将失败。您可以通过自己的方式确保一切都按预期进行。

答案 1 :(得分:1)

我认为那很好。构造函数在那里创建有用的对象。如果您确定没有设置这些对象就无法使用该对象,则没有理由不在构造函数中进行设置。

如果您选中https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

  

一个类包含构造函数,这些构造函数被调用以从中创建对象   课程蓝图。

您已经添加了字段,但是没有设置这些字段就没有工作对象,而且显然您已经知道这些值。最好的方法是将这些保留在构造函数中。如果有一些未知的值或要求来创建该类的实例,则可以考虑使用Factory模式或其他方式,但在这种情况下,构造函数的用法就可以了。