当前位置:  开发笔记 > 编程语言 > 正文

获取方法:一对多

如何解决《获取方法:一对多》经验,为你挑选了2个好方法。

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进行调用.



1> Stephan..:

为什么不重载getEmployeeName(??)方法?

getEmployeeName(int BatchID)
getEmployeeName(object SSN)(坏主意)
getEmployeeName(String Email)
等.

对我来说似乎是一个很好的"很多"方法.



2> jrudolph..:

你可以使用这样的东西:

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 allEmployees = null;
    for (Employee e:allEmployees)
        if (sel.matches(e))
            return e;
    return null;
}
public void usage(){
    findEmployee(byName("Gustav"));
    findEmployee(byBatchId(5));
}

如果通过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);
        }
    };
}

并使用它:

findEmployee(and(byName("Gustav"),byBatchId(5)));

你得到的是类似于CriteriaHibernate中的API.

推荐阅读
殉情放开那只小兔子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有