I have a java class SearchFilterWebPage
which represents a web page for an online shopping website. All of its (filter) methods allow you to set a filter, just like you would on a real shopping website. For example enterPriceRange(min, max), selectBrandsFilter(String...brands), and selectFreeShippingOnlyFilter()
. The below code is very simple and only prints messages.
I made a function applyMultipleFilters(FilterCommand [] functions)
which allows you to call any number of filter functions of SearchFilterWebPage
class. Each filter function is represented by an instance of FilterCommand
, i.e. the command pattern. For example, enterPriceRange(min, max), and selectBrandsFilter(String...brands)
are represented by EnterPriceRangeFilter and SelectBrandsFilter
classes respectively.
Basically, I made a class to represent each filter function. Can someone please tell me if this is a good idea ? Is there any other way besides reflection and the command pattern to implement applyMultipleFilters(...)
?
Class with main method :
public class SearchFilterWebPage {
public void enterPriceRangeFilter(int min, int max){
System.out.println("enterPriceRangeFilter");
}
public void selectBrandsFilter(String...brands){
System.out.println("selectBrandsFilter");
}
public void selectFreeShippingOnlyFilter(){
System.out.println("selectFreeShippingOnlyFilter");
}
//NOTE - Add 5 or more functions like the above ones !!!
//MAIN METHOD
public static void main(String [] main){
SearchFilterWebPage search = new SearchFilterWebPage();
FilterCommand [] filters = {
new EnterPriceRangeFilter(search, 50, 100),
new SelectBrandsFilter(search, "Adidash", "Nikki", "Pooma")
};
search.applyMultipleFilters(filters);
}
public void applyMultipleFilters(FilterCommand [] functions){
for(FilterCommand fun : functions){
fun.execute();
}
}
}
Output:
>>> EXECUTING : enterPriceRangeFilter
>>> EXECUTING : selectBrandsFilter
Supporting code:
Command:
public interface FilterCommand {
void execute();
}
Filter:
public class EnterPriceRangeFilter implements FilterCommand {
private final int min;
private final int max;
private final SearchFilterWebPage search;
public EnterPriceRangeFilter(SearchFilterWebPage search, int min, int max) {
this.search = search;
this.min = min;
this.max = max;
}
@Override
public void execute() {
//Use min, max.
System.out.print(">>> EXECUTING : " );
search.enterPriceRangeFilter(min, max);
}
}
Filter:
public class SelectBrandsFilter implements FilterCommand {
private final String [] brands;
private final SearchFilterWebPage search;
public SelectBrandsFilter(SearchFilterWebPage search, String...brands) {
this.search = search;
this.brands = brands;
}
@Override
public void execute() {
//Use brands.
System.out.print(">>> EXECUTING : ");
search.selectBrandsFilter(brands);
}
}
Aucun commentaire:
Enregistrer un commentaire