getEmployeeNameByBatchId(int batchID)
getEmployeeNameBySSN(Object SSN)
getEmployeeNameByEmailId(String emailID)
getEmployeeNameBySalaryAccount(SalaryAccount salaryAccount)
要么
getEmployeeName(int typeOfIdentifier,byte [] identifier) - >在这个方法中,typeOfIdentifier告诉标识符是否为batchID/SSN/emailID/salaryAccount
以上哪一种更好的方法实现了get方法?
这些方法将在Servlet中,并且将从将提供给客户的API进行调用.
为什么不重载getEmployeeName(??)方法?
getEmployeeName(int BatchID)
getEmployeeName(object SSN)(坏主意)
getEmployeeName(String Email)
等.
对我来说似乎是一个很好的"很多"方法.
你可以使用这样的东西:
interface Employee{
public String getName();
int getBatchId();
}
interface Filter{
boolean matches(Employee e);
}
public Filter byName(final String name){
return new Filter(){
public boolean matches(Employee e) {
return e.getName().equals(name);
}
};
}
public Filter byBatchId(final int id){
return new Filter(){
public boolean matches(Employee e) {
return e.getBatchId() == id;
}
};
}
public Employee findEmployee(Filter sel){
List<Employee> allEmployees = null;
for (Employee e:allEmployees)
if (sel.matches(e))
return e;
return null;
}
public void usage(){
findEmployee(byName("Gustav"));
findEmployee(byBatchId(5));
}
Run Code Online (Sandbox Code Playgroud)
如果通过SQL查询进行过滤,则可以使用该Filter接口组成WHERE子句.
这种方法的好处在于您可以轻松地将两个过滤器组合在一起:
public Filter and(final Filter f1,final Filter f2){
return new Filter(){
public boolean matches(Employee e) {
return f1.matches(e) && f2.matches(e);
}
};
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
findEmployee(and(byName("Gustav"),byBatchId(5)));
Run Code Online (Sandbox Code Playgroud)
你得到的是类似于CriteriaHibernate中的API.
| 归档时间: |
|
| 查看次数: |
673 次 |
| 最近记录: |