我有如下代码块:
@Repository
public class SummaryDao implements ISummaryDao {
public List<Response> reportOfCategory(RequestObject obj){
...
}
public List<Response> reportOfGroup(RequestObject obj){
...
}
public List<Response> reportOfSubGroup(RequestObject obj){
...
}
}
我的第二个存储库:
@Repository
public class ReportDao implements IReportDao {
public List<Response> reportOfCategory(RequestObject obj){
...
}
public List<Response> reportOfGroup(RequestObject obj){
...
}
public List<Response> reportOfSubGroup(RequestObject obj){
...
}
}
然后我从服务类中调用它们,如下所示:
@Service
public class ReportService {
@Autowired
ISummaryDao summaryDao;
@Autowired
IReportDao reportDao;
public List<Response> call(EnumType type, RequestObject obj){
if(type == Category) {
return obj.isSummary ? summaryDao.reportOfCategory : reportDao.reportOfCategory;
}
if(type == Group) {
return obj.isSummary ? summaryDao.reportOfGroup : reportDao.reportOfGroup;
}
if(type == SubGroup) {
return obj.isSummary ? summaryDao.reportOfSubGroup : reportDao.reportOfSubGroup;
}
...
}
如何有效地称呼他们?实际上,我想摆脱if-else块和Enum比较。
注意:枚举比较可以是大小写比较。
注意:在这段代码中,我使用了Spring-Boot EntityManager。我知道JPARepository。这个问题与此无关。
答案 0 :(得分:0)
我如下解决了它:
我创建了一个IGeneralDao以使用通用界面。
@Repository
public class SummaryDao implements IGeneralDao {
public List<Response> reportOfCategory(RequestObject obj){
...
}
public List<Response> reportOfGroup(RequestObject obj){
...
}
public List<Response> reportOfSubGroup(RequestObject obj){
...
}
}
第二个存储库:
@Repository
public class ReportDao implements IGeneralDao {
public List<Response> reportOfCategory(RequestObject obj){
...
}
public List<Response> reportOfGroup(RequestObject obj){
...
}
public List<Response> reportOfSubGroup(RequestObject obj){
...
}
}
要调用它:
@Service
public class ReportService {
@Autowired
IGeneralDao reportDao, summaryDao;
public List<Response> call(EnumType type, RequestObject obj){
IGeneralDao generalDao = isSummary ? summaryDao : reportDao;
switch(type):
case Category:
return generalDao.reportOfCategory;
case Group:
return generalDao.reportOfGroup;
case SubGroup:
return generalDao.reportOfSubGroup;
...
}
要摆脱枚举比较:
public interface IGeneralDao {
<T> T callReports(RequestDto requestDto);
IGeneralDao setRequestType(EnumType type);
}
第一个存储库:
@Repository
public enum ReportDao implements IGeneralDao {
Category {
@Override
public List<Response> callReports(RequestDto requestDto) {
return ...;
}
},
Group {
@Override
public List<Response> callReports(RequestDto requestDto) {
return ...;
}
},
SubGroup {
@Override
public List<Response> callReports(RequestDto requestDto) {
return ...;
}
};
@Override
public IGenericDao setEnumType(EnumType type) {
return valueOf(type.name());
}
}
第二个存储库:
@Repository
public enum SummaryDao implements IGeneralDao {
Category {
@Override
public Summary callReports(RequestDto requestDto) {
return ...;
}
},
Group {
@Override
public Summary callReports(RequestDto requestDto) {
return ...;
}
},
SubGroup {
@Override
public Summary callReports(RequestDto requestDto) {
return ...;
}
};
@Override
public IGenericDao setEnumType(EnumType type) {
return valueOf(type.name());
}
}
然后从主服务调用它:
@Service
public class ReportService {
@Autowired
IGeneralDao reportDao, summaryDao;
public List<Response> call(EnumType type, RequestObject obj){
IGeneralDao generalDao = isSummary ? summaryDao : reportDao;
return generalDao.setEnumType(type).callReports(obj);
}