We have the following method.
//Service class
public List<String> getStrings(Company company) {
List<String> list = new ArrayList<String>();
list = companyDao.findStrings("a");
if(CompanyName.PEPSI.equals(company.getName()) && list = 0) {
companyDao.findStrings("b");
}
return list
}
If query doesn't find "a" then search for "b" when the company is "Pepsi". So if our request for "Pepsi" will find results for "a", then there is no need to find and add (to the list) results from "b".
As you can see there is "If", this is ugly.
I would like to use strategy or factory (in future, cases such as the above can be more).
I do like this
public class MyFactory {
private static final Map<Company, BasicClass> MYMAP = Maps.newHashMap();
static {
MYMAP.put(CompanyName.PEPSI, new FindAorB());
}
public static BasicClass create(CompanyName name)
return MYMAP.containsKey(name) ? MYMAP.get(name) : new DefaultClass();
}
The solution probably ok, but can be there repeating code (Pepsi Company uses a default algorithm to search only with additional condition).
or
using strategy
public class SomeCompanyStrategy extends BasicStrategy {
@Override
public BasicClass getFindMethod() {
return new FindAorB();
}
}
How do I solve this problem? Apply as above solutions or something else?
Aucun commentaire:
Enregistrer un commentaire