I have the following factory class, wonder if this code can be implemented in better way. I saw few discussions about spring factory bean, but don't find good examples.
@service
public CustomerSearchFactory{
@Autowired
private CustomerSearchByID searchById;
@Autowired
private CustomerSearchByName searcyByName;
@Autowired
private CustomerSearchBySSN searchBySSN;
public CustomerSearch getInstance(Request param){
if (StringUtils.isNotBlank(param.getID()){
return searchById;
}else if (StringUtils.isNotBlank(param.getName()){
return searcyByName;
}else if (StringUtils.isNotBlank(param.getSSN()){
return searchBySSN;
}
}
}
@service
public CustomerSearchByID implements CustomerSearch{
@Autowired
private Service1 service1;
public Response search(Request request){
--
--
ReturnResponse rs = service1.performSearch(inputRequest);
--
return response;
}
}
@service
public CustomerSearchBySSN implements CustomerSearch{
@Autowired
private Service2 service2;
public Response search(Request request){
--
--
ReturnResponse rs = service2.performSSNSearch(inputRequest);
--enter code here`
return response;
}
}
caller code:
CustomerSearch customerSearch= customerSearchFactory.getInstance(param)
response = customerSearch.search(request);
I am using the spring 3.1 framework and want to know if this code can be optimized.
1 Answer 1
I would advise not to use a different CustomerSearch
per search criteria.
As your functionality will evolve you could need a CustomerSearch
that is using the country the Customer
is from.
But what if you want to search on the Country
in combination with part of the name?
Soon you will be combining different search criteria together which will require one smart CustomerSearch
bean.
-
\$\begingroup\$ Malachi, The reason for having different customer search is to invoke different services. I don't think we will have another search in the application and the search can be done only on one criteria. let know if you have any suggestion. \$\endgroup\$skumar– skumar2014年01月08日 23:44:34 +00:00Commented Jan 8, 2014 at 23:44