Why using Interface for Search Feature?

Can anyone explain why using Interface for Search feature?

public interface Search {
public List searchByTitle(String title);
public List searchByAuthor(String author);
public List searchBySubject(String subject);
public List searchByPubDate(Date publishDate);

public class Catalog implements Search {
private HashMap<String, List> bookTitles;
private HashMap<String, List> bookAuthors;
private HashMap<String, List> bookSubjects;
private HashMap<String, List> bookPublicationDates;

public List searchByTitle(String query) {
// return all books containing the string query in their title.
return bookTitltes.get(query);

public List searchByAuthor(String query) {
// return all books containing the string query in their author’s name.
return bookAuthors.get(query);

I don’t understand why we use interface here because It seems like don’t reuse this interface anywhere even for an upgrade in future?



Thanks for reaching out to us. We are looking into your query and will get back to you soon.

Please let us know if you have any further queries.

Same question here. Why would we use an interface for searching? I noticed this design is also made in the car rental system.

Hi @DuHDH and @coderdude,

Thanks for posting your question. Interface is used here to represent another view of the underline class. Take the example of Library Management System - here ‘Member’ and ‘Librarian’ are interested only in searching the ‘Catalog’ without doing any modification to it, so we’ve created an interface to expose a search view for these users. Another implementation can be to expose static methods in ‘Catalog’ for searching.

We also intend to make Catalog a Singleton, as we wanted to have only one object of ‘Catalog’ in the system so that all updates should go through that object only. In this case, ‘Member’ and ‘Librarian’ can be passed the interface of the ‘Catalog’ object.

Hope this clarifies your question.

@Design_Gurus I have further question to your answer. What is the significance of declaring HashMap<key, List> for each type of query search? How and when will the hashmap variables be updated?