mardi 25 octobre 2016

Design Pattern to be used for supporting different filter set on a student relational table in JAVA

I have a SQL table with schema

[student_id, standard, home_city, home_state, country, num_of_subjects]

Now, I want to support api end point over this table as below:

GET /students?home_state=?&country=?&home_city=?&standard=? 

All the filters are optional.

Now, I want to know a good code design for this requirement. For technology I am using JAVA for web-app and a library querydsl for runtime query construction.

I have some ideas floating, but looking for a concrete and extensible approach, which can handle cases, so when a new filter column is added(let us say fav_subject), it should be minimum code change.

For ideas, I am thinking of a Filter interface with a method

public interface Filter { 
   void addFilter(Map<String, String> apiCallParameters, List<QueryFilters> addToList); //if apiCallParameters contains this filter key ,add filter to addToList
}

, all possible filters will be a class each, implementing this interface. Now, the query construction class can have List<Filter> , with each having addFilter deciding whether this filter will be added or not.

I want to better understand how this seemingly very common case is handled in code design.

Aucun commentaire:

Enregistrer un commentaire