Code solution or Design pattern for avoiding if else condition
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Code solution or Design pattern for avoiding if else condition

  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Code solution or Design pattern for avoiding if else condition

    Hi Team,
    I am wondering if anyone can suggest me a design pattern or best way to code the below problem.

    1) I have an array list of books like the below
    list.add(new Book(title, author);
    list.add(new Book(title1, author1);
    and so on....

    2) And now I would like to find all the books from the list by author

    findByAuthor(String author) {
    for(Book book : list){
    if(book.getAuthor().equals(author)){
    return book;
    }
    }
    }

    Like wise I have another method called findByTitle(). But, it would be same code except book.getAuthor() will have to be book.getTitle(). Everything will be same.

    3) Now i can write a method which is generic to both methods like below;

    findByBookProperty (String type, String propertyValue){
    for(Book book : list)
    if(type.equals("author") && book.getTitle().equals(propertyValue)){
    return book;
    } //another else if for author
    //another else for another property
    // if else repeats for all the required finder types...
    }
    }

    4) The problem i have here is;
    1. I dont want to use the nasty if/else condition for the finder types.
    2. I want to know if there is any design pattern or better way to handle this if else or swich method.

    Important note: I get the author name as a request parameter value in my spring controller method.

    I appreciate your thoughts.

    Many thanks,
    Raj

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Code solution or Design pattern for avoiding if else condition

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center