3
\$\begingroup\$

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.

asked Jan 8, 2014 at 0:13
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

Malachi
29k11 gold badges86 silver badges188 bronze badges
answered Jan 8, 2014 at 14:34
\$\endgroup\$
1
  • \$\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\$ Commented Jan 8, 2014 at 23:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.