0
以下代码是长参数列表气味方法。长参数列表,其中一组参数可以分组到现有类中,但这些参数不是从对象中收集的
// Long Parameter List Smell
public void paint (Graphics gr,
double x, double y, double width, double height,
Boolean shouldValidate){.....}
下面的代码是调用上述气味的调用方法。
// Caller
public void someMethod(......){
double x = someMethod();
double y = 200;
double width = 100;
double height = width/2;
paint(gr, x, y, width, height, true);
}
我们可以注意到参数双重的x,双Y,双宽度和双倍高度声如矩形对象。这些参数不是从对象收集的,所以我们不能使用保留整个对象。下一个选项是介绍参数对象。但是,已经存在类库Rectangle2D.Double,它是Java库中的类。
我应该将这些参数分组到Rectangle2D.Double的对象吗?重构的代码将如下所示。如果可以通过这种方式进行重构。我可以将这种类型的重构称为什么?
// Long Parameter List Smell
public void paint (Graphics gr,
Rectangle2D.Double rec
Boolean shouldValidate){.....}
// Caller
public void someMethod(......){
double x = someMethod();
double y = 200;
double width = 100;
double height = width/2;
paint(gr,
new Rectangle2D.Double(x, y, width, height),
true);
}