1
我想实现的是匹配多个布尔选项匹配条件。到目前为止,这是我的设计。我对此不满意。如何高效地写入与条件匹配的多项选项?或者是有多个选项的主要代码气味?匹配多个选项到特定条件
class Condition
{
Boolean a; // Optional
Boolean b; // Optional
Boolean c; // Optional
boolean d; // mandatory
Condition(Boolean a, Boolean b, Boolean c, boolean d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public boolean matches(Boolean a, Boolean b, Boolean c, boolean b)
{
return (a == null || this.a == a)
&& (b == null || this.b == b)
&& (c == null || this.c == c)
&& (this.b == b);
}
public static void main(String args[]) {
Condition c1 = new Condition(true, true, null, true);
Condition c2 = new Condition(null, null, false, false);
Condition c3 = new Condition(false, true, null, true);
Condition[] conditions = new Condition[]{c1,c2,c3};
for (Condition con : conditions) {
Boolean a = someTestLogic1(...);
Boolean b = someTestLogic2(...);
Boolean c = someTestLogic3(...);
boolean d = someTestLogicMandatory(...);
if (con.matches(a,b,c,d)) {
// found the matching condition
return con;
}
}
}
}
你能否澄清一下 - 特别是这段代码的哪部分是你不满意的? –
我怀疑有更高效的(速度)和优雅的方式(少冗长)实现相同的较小的构造函数参数,我也不喜欢循环的所有条件和打破找到第一匹配 – delita
是方法在'for'循环中调用'someTestLogic1()'等等,是不是?原因是,所有这些方法似乎都是零参数方法,那么它们如何“返回”差异。 '布尔值'?我的意思是他们可以在循环之外吗? – developer