Java - How to design a method for search? -


i designing decorator class wraps news search engine, can ship class library used other teams.

the exposed interface of class java-styled one, takes lot of parameters, decorator assembles these parameters search text search engine.

my method following:

public list<news> search(list<string> keywords, list<string> categories, list<string> authors, list<location> locactions, list<localdate> dates, list<sortrule> sortrules, int offset, int limit); 

yes, know... method looks ridiculously long, error-probe , hard use clients.

so, how can design better api such search functionality?

you can try writing wrapper class applying rule filters , take new object of class in current class. way simple , cleaner.

for example,

rulefilters r = new rulefilters(); r.addfilters(type.keywords, keywords); r.addfilters(type.categories, categories); r.addfilters(type.authors, authors); 

here type enum holds details of different rule filters {categories, authors, keywords} etc.

finally in main class:

public list<news> search(rulefilters r) {     /* rule filters */  }; 

interface:

list<news> search(rulefilters r); 

note: public keyword not necessary in interfaces.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -