내가 달성하려는 것은 일치 조건에 여러 부울 옵션을 일치시키는 것입니다. 지금까지 이것은 나의 디자인이다. 나는 그것에 만족하지 않는다. 하나의 조건과 일치하는 여러 옵션을 효율적으로 작성하는 방법은 무엇입니까? 아니면 여러 옵션에 주요 코드 냄새가 있습니까?특정 조건에 대해 여러 옵션 일치
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;
}
}
}
}
이 코드의 어느 부분이 특히 불만입니까? –
나는 더 적은 Constructor params를 사용하여보다 효율적이고 (속도가 빠르며) 우아한 방법 (덜 장황 함)을 보인다. 또한 모든 조건을 반복하고 첫 번째 일치하는 것을 찾지 않아도된다. – delita
메서드는'someTestLogic1()'등을 호출합니다. for 루프 안에 ... 맞습니까? 그 이유는 모든 메소드가 zero-arg 메소드처럼 보이고 어떻게 diff를 리턴 할까하는 것입니다. '부울 '값? 루프 외부에있을 수 있습니까? – developer