我的例子是這樣的我希望我的用戶呼叫在java中我如何設計一個API既普通,簡單,可讀
findCustomers(...)
現在我的問題是關於參數此方法。 我有一個
Customer.java
對象,我希望用戶能夠使用圖書館的客戶名稱和客戶ID進行搜索。
現在我不想創建多個方法
findCustomerById(String id)
findCustomerByName(String name)
findAllCustomers()
相反,我以爲這樣做的,這是一個通用的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)
現在我有一個通用的單一方法的API,但我不我不喜歡這種方式,我把零件傳給了一個物體,我不喜歡零點。
任何人都有這樣一個API的明確最佳做法?
感謝
+1使用新類型的CustomerQuery而不是客戶 –
現在我看到這一點,你甚至可以建立一個查詢工廠,以避免這個尷尬的構造函數調用: QueryFactory.customer()。withName(「john(。*) 「).olderThan(10) QueryFactory.customer()將是靜態工廠方法,返回CustomerQueries。那麼您可以重複使用相同的查詢工廠進行任何其他您可能擁有的實體查詢 – radai