문제

내가 생각하는 코드로 가장 잘 설명 된 것은 간단한 예일뿐입니다.

public class MyPOJO {

    public String name;
    public int age;

    public MyPOJO(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class MyProcessor {

    public List<MyPOJO> process(List<MyPOJO> mypojos) {
        List<MyPOJO> temp = new ArrayList<MyPOJO>; 
        for (int i=0; i <moypojos.size(); i++) {
            if (filterOne(mypojos[i])) continue;
            if (filterTwo(mypojos[i])) continue;
            if (filterThree(mypojos[i])) continue;
            temp.add(mypojos[i];
        }
    }

    public boolean filterOne(MyPOJO mypojo) {
        // in practice filters aren't so basic
        return (mypojo.age < 21);
    }
    // assume implementations for the other filter methods
}

이크 스가 추악 해요. 기본적으로 나는 컬렉션을 가지고 있으며 특정 기준을 충족하는 객체 만 계속 처리하기 위해 일종의 체를 통과하고 싶습니다. 내 생각에 부울을 반환하는 많은 방법보다 더 나은 패턴이 있다는 것입니다.

도움이 되었습니까?

해결책

목록을 가질 수 있습니다 IFilters.

그렇게 좋아요

boolean filtersResult = false;
for (IFilter filter : filters) {
    filterResult = filter.process(mypojos[i])

    if (filterResult)
        break;
}

다른 팁

필터가 컬렉션을 취하고 필터링 된 컬렉션을 반환 할 수 있도록 필터를 구현할 수 있습니다.

public interface IFilter<E> {
  public Collection<E> filter(Collection<E> input);
}

이렇게하면 매우 사소하게 필터를 함께 체인 할 수 있습니다. 단점은 큰 컬렉션의 경우 느리게 느리고 더 많은 공간을 차지한다는 것입니다. 그러나 코드는 훨씬 더 읽기 쉽습니다.

사용하지 않는 이유 콩 쿼리? 코드를 읽을 수있게 할 수 있습니다.

List<MyPOJO> result=selectBean(MyPOJO.class).where(
                                                not(
                                                  anyOf(
                                                      value("aga",lessThan(21)),
                                                      value("age",greaterThan(50))
                                                  )
                                                )
                                            .executeFrom(myPojos).
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top