I am currently trying to refactor my code after I read up that implementations are preferred over extensions. Currently, I am trying to create a function that adds an Object into a Scene. In order to better define what each object is, there are multiple Lists such as a list for updating, rendering, etc.
private List<Updatable> updatables;
private List<Removable> removables;
private List<Renderable> renderables;
private List<Collidable> collidables;
I want to make a function into my Scene class like so:
public void add(Object o) {
if(o instanceof Updatable)
updatables.add((Updatable) o);
if(o instanceof Removable)
removables.add((Removable) o);
if(o instanceof Renderable)
renderables.add((Renderable) o);
if(o instanceof Collidable)
collidables.add((Collidable) o);
}
likewise I would create a similar remove function. My question is if this is bad coding practice since I have heard of the pitfalls of instanceof, and secondly if it is, is there a way around this / restructuring my code to make adding instances simple? I prefer not to have to add an instance to every list and define which ones it belongs to every time.
I would do similar with Factory pattern using
Map