Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Jan 8 '14 at 0:15

This question came from our site for professional and enthusiast programmers.

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.

share|improve this answer
    
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. –  skumar Jan 8 '14 at 23:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.