Currently have an issue getting a specific object in an arraylist. So I have multiple classes that implements the same interface, and I create objects of the different classes. The problem is that I don't know how to differentiate the classes in the arraylist.
ArrayList<Interface> arraylist = new ArrayList<>();
public static void main(String[] args) {
addInterface(new interfaceA());
addInterface(new interfaceB());
addInterface(new interfaceC());
}
public static void addInterface(Interface foo) {
arraylist.add(foo);
}
Let say that I want to get interfaceA(), I could call it by arraylist.get(0) but I don't want to hardcode it. Each class has the same methods but the code is different.
Interface interface2. You can not instantiate an interfacenew interfaceA().Chain of Responsibilityand remove the specific attempt to retrieve it; allow each implementation to decide it wants to handle the request or not. If not possible, what are the criteria by which you will select? Perhaps aMapwith the appropriate key. Finally, consider looking at this question Type Handler Map which, leaving aside the generics and discussion on Exceptins, shows something similar, which is a registry.