Reputation: 5240
I want to have a search class with several different options in it, my search class should be able to filter results in different ways such as :
getX()
getY()
getZ()
getK()
above X,Y,Z,K are my criterion and they are common in my case so I decided to create an interface like following :
public interface IFilter {
public String getX();
public void setX(String x);
.
.
.
//I am not sure about the return type of my criteria!
public IAmNotSureAboutReturnType criteria();
}
the application should be capable of geting 1 or more criteria at each time and my Idea is to have an interface to instruct a class to implement all the criterion and all a criteria() method to return back a compiled criteria to my search class.
All of the criterion are based on String but yet I'm not sure about return type of criteria() since it should combine all the given criterion and return one particular type as return value.
and my search class is not based on SQL its mostly based on JSON.
Can anyone suggest me what is the best approach to have an interface for criterion for a search class?
Upvotes: 1
Views: 396
Reputation: 5220
Maybe visitor pattern will help for filter implementation:
public interface IFilter<T> {
/**
* This method will be called by filtering mechanizm. If it return true - item
* stay in resul, otherwise item rejected
*/
boolean pass(T input);
}
You can create AND and OR filters that will combine more than one filter:
public class AndFilter<T> implements IFilter<T> {
private final List<IFilter<T>> filters;
public AndFilter(List<IFilter<T>> filters) {
this.filter = filter;
}
@Override
public boolean pass(T input) {
for(IFilter<T> filter : filters) {
if(!filter.pass(input)) {
return false;
}
}
return true;
}
}
Upvotes: 2