我的例子是我希望我的用户打电话
findCustomers(...)
Run Code Online (Sandbox Code Playgroud)
现在我的问题是关于这种方法的论点.我有一个
Customer.java
Run Code Online (Sandbox Code Playgroud)
对象,我希望用户能够使用库按客户名称和客户ID进行搜索.
现在我不想创建多个方法
findCustomerById(String id)
findCustomerByName(String name)
findAllCustomers()
Run Code Online (Sandbox Code Playgroud)
相反,我想做的是这是一个通用的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)
Run Code Online (Sandbox Code Playgroud)
现在我有一个api的通用单一方法,但我不喜欢我在一个对象中传递空值,我不喜欢空值.
对于这样的api,任何人都有明确的最佳实践吗?
谢谢
怎么样的东西像流量api查询:
List<Customer> matches = find(new CustomerQuery().withName("john(.*)").withId(42));
Run Code Online (Sandbox Code Playgroud)