我有以下代码,基于构造函数的输入,我需要初始化一些包含在简单POJO中的值。这些值是常量,如您所见。
但是,我不喜欢if / else构造,并且已经阅读了NavigableMap作为保持范围值的替代方案。有关如何改进/清理下面的内容或者确实使用此构造的任何想法?
由于
private Calibrated calibratedValues;
public CalibrationProperties(long odNumber) {
setCalibratedValues(odNumber);
}
private void setCalibratedValues(long odNumber) {
if (odNumber < 762) {
this.calibratedValues = new Calibrated(K0, H0, K0_INV, H0_INV, F0);
} else if (odNumber < 866) {
this.calibratedValues = new Calibrated(K1, H1, K0_INV, H0_INV, F1);
} else if (odNumber < 1011) {
this.calibratedValues = new Calibrated(K2, H2, K2_INV, H2_INV, F2);
} else {
this.calibratedValues = new Calibrated(K3, H3, K3_INV, H3_INV, F3);
}
//Two exceptions
if (odNumber == 858){
this.calibratedValues = new Calibrated(K2, H2, K2_INV, H2_INV, F2);
}
if (odNumber == 1005){
this.calibratedValues = new Calibrated(K3, H3, K3_INV, H3_INV, F3);
}
}
public Calibrated getCalibratedValues() {
return calibratedValues;
}
/** Convenience class used to hold calibrated values */
static class Calibrated {
private double[] k;
private double[] h;
private double[] kinv;
private double[] hinv;
private double f;
Calibrated(double[] k, double[] h, double[] kinv, double[] hinv, double f) {
this.k = k;
this.h = h;
this.kinv = kinv;
this.hinv = hinv;
this.f = f;
}
public double[] getK() {
return k;
}
public double[] getH() {
return h;
}
...
答案 0 :(得分:0)
你没有在构造函数中使用if / else,但是你已经创建了类似工厂方法的东西。
示例:
public static Calibrated createSmallCaribration(){
return new Calibrated(K0, H0, K0_INV, H0_INV, F0);
}
如果你这样做,你甚至可以看到你的构造包。你会知道每个构造函数配置是什么,