Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in java how do i design an api to be both generic, simple, readable [closed]

Tags:

java

oop

api

My example is this I want my user to call

findCustomers(...)

now my question is about the argument to this method. I have a

Customer.java

object and I would like the user to be able to use the library to search by customer name, and customer id.

now i would not want to create multiple methods

findCustomerById(String id)
findCustomerByName(String name)
findAllCustomers()

Instead what I thought of doing is this a generic findCustomer

/** if you pass only custoer.name and rest of fields are null will search by name,
if you pass a null custoer object will return all customers, if you pass both custoer id and his name will search by both fields). **/
findCustomer(Customer customer)

Now I have a generic single method for api but i don't like that i pass nulls in an object, i don't like nulls.

anyone has a clear cut best practice for such an api?

thanks

like image 471
Jas Avatar asked Dec 15 '22 16:12

Jas


1 Answers

how about something like a fluid api for queries:

List<Customer> matches = find(new CustomerQuery().withName("john(.*)").withId(42));
like image 93
radai Avatar answered Mar 29 '23 23:03

radai