One way to solve this type of problem is to define an interface with a single method which takes a single string argument eg
Code:
interface BookFinder
{
Book getBook(String value) ;
}
Then pass into your findByBookProperty method an instance of an inner class (or anonymous inner class) that implements this interface. The findByBookProperty method calls the getBook() method of the passed in object which then takes the appropriate action to find the book and return it. eg: to find a book by author you could use an anonymous inner class as follows:
Code:
findByBookProperty(new BookFinder(){
Book getBook(String value) {
return findByAuthor(value);
}, "Ian Flemming");
The findByBookProperty method would look like:
Code:
Book findByBookProperty(BookFinder finder, String value) {
finder.getBook(value);
}
Another approach is to use enums to define the types of search you can do and implement the specific search method.
Bookmarks