In the code below, I don’t want to use instanceof to know the type of repository.
Is it the only way to get the type? Is it possible to know de type with the wildcard RepositoryVet<?>? This would save me doing lots of test.
public ArrayList<String> rechercherTabTailles(String sexe,String SI,RepositoryVet<?> repository) {
ArrayList<String> tabTailles;
repository.Open();
repository.rechercherspin(sexe);
if (SI.compareTo("US") == 0) {
if (repository instanceof ChaussureRepository)
tabTailles= ((ChaussureRepository) repository).taillesUS;
if (repository instanceof ChemiseRepository)
tabTailles= ((ChemiseRepository) repository).taillesUS;
}
}
To complete my question :
RepositoryVet is already an abstractClass defined like this :
public abstract class RepositoryVet implements IRepositoryVet {
public ArrayList<String> taillesEU;
public ArrayList<String> taillesUS;
public ArrayList<String> taillesUK;
public ArrayList<String> taillesIT;
public ArrayList<String> taillesJP;
public ArrayList<String> taillescm;
public ArrayList<String> taillesinch;
etc….
}
And “ChaussureRepository” is a class that extends RepositoryVet like this :
public class ChaussureRepository extends RepositoryVet {
public ArrayList<String> taillesEU;
public ArrayList<String> taillesUS;
public ArrayList<String> taillesUK;
public ArrayList<String> taillesIT;
public ArrayList<String> taillesJP;
public ArrayList<String> taillescm;
public ArrayList<String> taillesinch;
etc….
Well, you could use a property in the repository to represent the type, e.g. an enum.
You’d still need some tests though.
However, in your case, inheritance might be the better approach: create a common interface that provides a method
List<String> getTaillesUS()(or maybeList<String> getTailles(TailesType tailesType)(whereTailesTypewould be an enum with at least the valueUS).