As a simplified example, consider a ListView that can contain both sub-categories and book titles. If a book title is clicked, a new activity should start that shows the cover image. If a sub-category is clicked, a new list of books and categories is displayed.
A Row interface is defined as follows.
interface Row {
void onClick();
void draw(View v);
}
I would like to know how to prevent a dependency from the ListView‘s ArrayAdapter as well as from the implementer of onItemClickListener on the Row implementers (e.g.,Book and Category).
One of the forces driving this requirement is the “don’t repeat yourself” (DRY) principle: the ArrayAdapter implementation does not need to change when new row types are introduced.
Forgive my chicken-scratched “UML” below, but here’s how I do it.
The implementer of
OnItemClickListenerhas a method like this:Then
RowProviderhas a method like this:Then the
Rowinterface has a method with signaturevoid onClick()and there areCategoryandBookimplementations ofRowthat provide the necessary behavior.