|
Searching Criteria
Listing 2. SearchForm now implements SearchCriteria as a marker interface. SearchCriteria declares abstract methods matching the methods in SearchForm. The signature of the find() method in SearchFacade now takes SearchCriteria, and Java's language support for polymorphism provides the type conversion from SearchForm to SearchCriteria. SearchAction is unchanged and is no longer tightly bound to SearchFacade. public class SearchForm extends ActionForm implements
SearchCriteria{
private String keyword;
private Date start;
private Date end;
// accessor and mutator methods elided
}
public interface SearchCriteria {
// accessor methods required by our example
public abstract String getKeyword();
public abstract Date getStartDate();
public abstract Date getEndDate();
}
...
public class SearchFacade{
public Collection find(
SearchCriteria searchCriteria){
Collection collection = null;
// SearchCriteria is not a subclass of
// ActionForm and SearchFacade is no longer
// bound to Struts
String keyword = searchCriteria.getKeyword();
Date startDate = searchCriteria.getStartDate();
Date endDate = searchCriteria.getEndDate();
// execute a search on the data source
return collection;
}
}
|