I have a class (Timer
) with an array list of Timable
objects. Timeable
is an interface. There is some specific functionality that I need for the Trigger
class (implements Timable
), which has a reference called target
. A lot of methods need to search through the Timer
array for Trigger
objects with a certain target
.
What I did was a function like this:
public Timable[] findObjectsWithTarget(Cue target) {
ArrayList<Timable> result = new ArrayList<Timable>();
for (Timable timed : fireable) { //fireable is the array (actually a HashSet) of Timed objects
if (timed instanceof Trigger && ((Trigger) timed).getTarget() == target)
result.add(timed);
}
return (Timable[]) result.toArray();
}
This feels like a bad practice. The Timer class is now dependent on the Trigger class (sort of) and are no longer generalized.
So, my two questions:
1) Is it actually a bad practice? Why or why not?
2) What's a better way if it is a bad practice?