我认为最好用代码来解释,这只是一个简单的例子:
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
}
哎呀,太丑了。基本上我有一个集合,我想通过各种筛选来仅继续处理满足特定条件的对象。我的猜测是有比一堆返回 boolean 值的方法更好的模式。
最佳答案
您可以拥有 IFilters
列表。
像这样
boolean filtersResult = false;
for (IFilter filter : filters) {
filterResult = filter.process(mypojos[i])
if (filterResult)
break;
}
关于java - 在数据集合上运行流程的良好设计模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2427048/