patternjavaspringMinor
factory pattern in spring
Viewed 0 times
factoryspringpattern
Problem
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.
caller code:
I am using the spring 3.1 framework and want to know if this code can be optimized.
@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.
Solution
I would advise not to use a different
As your functionality will evolve you could need a
But what if you want to search on the
Soon you will be combining different search criteria together which will require one smart
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.Context
StackExchange Code Review Q#38802, answer score: 3
Revisions (0)
No revisions yet.