为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

月关宝盒 2019-05-10 16:04:24
有两个类结构差不多,都在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]querydbresult=customerRepository.findxxx(ids,location);}else{//use[ids]querydbresult=customerRepository.findxxx(ids);}}else{if(locationCk){//use[location]querydbresult=customerRepository.findxxx(location);}else{//queryallrecordindbresult=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]querydbresult=clientRepository.findxxx(ids,location);}else{//use[ids]querydbresult=clientRepository.findxxx(ids);}}else{if(locationCk){//use[location]querydbresult=clientRepository.findxxx(location);}else{//queryallrecordindbresult=clientRepository.findxxx();}}}catch(Exceptione){thrownewSystemException();}returnresult;}
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 527 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信