猿问

怎么用自定义泛型实现下面的代码,怎么优化

有两个类结构差不多,都在Order类里面用到,而且方法里面逻辑也差不多,怎么优化
因为Customer和Client不同类型,所以写了两个方法,只是传的参数不一样
自定义泛型可以实现么,怎么写比较好,或者其它有什么方式
谢谢
publicclassCustomer{
privateStringlocation;
privateBooleanisDone;
//getsettoString...
}
publicclassClient{
privateStringlocation;
privateBooleanisDone;
//getsettoString...
}
publicclassOrder{
publicvoidexecute_customer(Customercustomer){
if(customer.getDone()){
customer.setLocation("hongkong");
}
System.out.println("result:"+customer);
}
publicvoidexecute_client(Clientclient){
if(client.getDone()){
client.setLocation("hongkong");
}
System.out.println("result:"+client);
}
}
=============================情况二=====================================
publicListretrieveEntityRecords(RequestParamrequest){
Listids=request.getId();
Stringlocation=request.getLocation();
booleanidsCk=CollectionUtils.isNotEmpty(ids)&&ids.size()>0;
booleanlocationCk=!StringUtils.isEmpty(location);
Listresult;
try{
if(idsCk){
if(locationCk){
//use[idslocation]querydb
result=customerRepository.findxxx(ids,location);
}else{
//use[ids]querydb
result=customerRepository.findxxx(ids);
}
}else{
if(locationCk){
//use[location]querydb
result=customerRepository.findxxx(location);
}else{
//queryallrecordindb
result=customerRepository.findxxx();
}
}
}catch(Exceptione){
thrownewSystemException();
}
returnresult;
}
publicListretrieveEntityRecords(RequestParamrequest){
Listids=request.getId();
Stringlocation=request.getLocation();
booleanidsCk=CollectionUtils.isNotEmpty(ids)&&ids.size()>0;
booleanlocationCk=!StringUtils.isEmpty(location);
Listresult;
try{
if(idsCk){
if(locationCk){
//use[idslocation]querydb
result=clientRepository.findxxx(ids,location);
}else{
//use[ids]querydb
result=clientRepository.findxxx(ids);
}
}else{
if(locationCk){
//use[location]querydb
result=clientRepository.findxxx(location);
}else{
//queryallrecordindb
result=clientRepository.findxxx();
}
}
}catch(Exceptione){
thrownewSystemException();
}
returnresult;
}
月关宝盒
浏览 528回答 2
2回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答