I have some interfaces that I intend third-parties to implement in the future, and I provide a base implementation myself. I'll only be using a couple to show the example.
Currently, they are defined as
Item:
public interface Item {
String getId();
String getName();
}
ItemStack:
public interface ItemStackFactory {
ItemStack createItemStack(Item item, int quantity);
}
ItemStackContainer:
public interface ItemStackContainer {
default void add(ItemStack stack) {
add(stack, 1);
}
void add(ItemStack stack, int quantity);
}
Now, Item
and ItemStackFactory
I can absolutely foresee some third-party needing to extend it in the future. ItemStackContainer
could also be extended in the future, but not in ways that I can foresee, outside my provided default implementation.
Now, I'm trying to make this library as robust as possible; this is still in the early-stages (pre-pre-alpha) so this may be an act of over engineering (YAGNI). Is this an appropriate place to use generics?
public interface ItemStack<T extends Item> {
T getItem();
int getQuantity();
}
And
public interface ItemStackFactory<T extends ItemStack<I extends Item>> {
T createItemStack(I item, int quantity);
}
I fear that this may end up making implementations and usage more difficult to read and understand; I think it's the recommendation to avoid nesting generics wherever possible.