jeudi 16 novembre 2017

Using filter design pattern for dynamically creating a filter criteria

I have a requirement to search a collection of objects (say, reservations) based on one or more of the following search criteria. I'm planning to use the filter pattern to implement this.

Search options: firstname, lastname, phone, email

The requirement is - if the user specifies only the fistname filter, use only firstname to filter. If the user specifies both firstname and phone, use both (and criteria) to filter. If all 4 options are specified use all 4.

I've created the classes for all 4 search criterion. I'm in doubt as to how to use it. For example,

ISearchCriteria searchCriteria;

   if(firstName != null)
   {
            searchCriteria= new FirstNameSearchCriteria(CurrentClient);
   }

   if(lastName!= null)
   {
           //what do I do here? I have an AndCriteria class which accepts two ISearchCriteria objects. How do I use it?
   }

What's the correct way to handle this search scenario?

Aucun commentaire:

Enregistrer un commentaire