Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

When should I use the JAVA generic interface design?

Asked by Arun Singh Oct 11, 2022 1.5K views 1 answer
Share

About this question

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?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.