I want to have my models wrapped up in a ModelCollection which is mostly used for a ListView. The Collection has always the same attributes like title, totalResults (for Pagination) and it shall contain the listItem-Models in the ArrayList “items”.
However, these models have different types like “ModelCategory” or “ModelChain” and often have different Properties and Methods.
How can I achieve this in java with strong Typing discipline? I heart interface a the right way to do this. WHERE do I have to implement them?
public class ModelCollection {
public ArrayList< ModelCategory OR ModelChain OR ModelXyz> items = new ArrayList<ModelCategory OR ModelChain OR ModelXyz>();
private String id;
private String title;
private Long updated;
private String linkSelf;
private int totalResults;
private int startIndex;
/*
more stuff like parsing a feed
*/
}
Make your ModelCategory, ModelChain, and ModelXyz implement an interface. Then have your collection be on that interface.
For instance:
To reference specific methods of each class, you will need to cast your list objects to the correct type.
Obviously, you can use whatever methods you need to iterate through your collections.