2
假设我有以下的DAO接口:指定订购的DAO方法
public interface CountryData {
/**
* Get All Countries.
*
* @return A Collection of Countries.
* @throws DataAccessException Thrown if there is an error communicating with the data store. The Exception's
* Cause Exception can usually be examined to determine the exact nature of the
* error.
* @since 1.0.0
*/
public List<Country> getAll();
}
进一步假设,我抽象这个DAO,因为我可以提供2级的实现,一个数据库,一个用于Web服务。
我想重载getAll方法来接受某种排序参数来指示如何排序返回的值。
但我不想将接口绑定到特定的实现。例如,数据库实现将使用ORDER BY子句,并且需要数据库列的列表以及诸如“ASC”或“DESC”的订单方向,其中Web服务实现不在此处。
提供这样一个参数而不将调用者耦合到特定实现的最佳实践是什么?
编辑
小澄清我的问题。我不只是想指定一个参数来指示顺序的方向,而且还要什么来订购。
例如,假设我的国家模型,定义如下:
public final class Country implements Serializable {
private int id;
private String name;
public Country() {
}
public Country(Country countryToCopy) {
this.id = countryToCopy.getId();
this.name = countryToCopy.getName();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
而且我想返回值由升序名订购。如我所建议的那样,我可以使用ENUM作为顺序方向,但是如果指定属性进行排序而不公开实现细节,那么最佳方法是什么?
请解释'SortOrder.RANDOM'的工作原理。 –
通过ENUM指定顺序方向实际上是我想到的,但是如何指定模型上的哪些属性进行排序而不实现特定的实现(例如,将数据库列的名称传递给DAO)?我编辑了我的问题以反映我需要的内容。感谢您的快速回答。 – 9ee1