Probably kind of a beginner question but I am stuck in my box.
Assuming the following interface:
public interface Foo {
void one() throws Exception;
void two() throws Exception;
}
And this class:
class MyClass {
private Collection<Foo> foos;
MyClass(Collection<Foo> foos) {
this.foos = foos;
}
public void oneOnAllFoos() {
// assuming more code...
for (Foo foo : foos) {
// assuming more code...
foo.one(); // the only different line
}
}
public void twoOnAllFoos() {
// assuming more code...
for (Foo foo : foos) {
// assuming more code...
foo.two(); // the only different line
}
}
}
Now in case the oneOnAllFoos and twoOnAllFoos are the same except for the foo one() and two() calls, how can I refactor MyClass to get one method containing all logic letting me specify which method on the Foo objects to be called? I know it is possible using reflection but I think there must be a KISS way, too. Thanks!
Edit: added throws Exception to the interface methods.
Edit2: the // assuming more code... contains the exception handling of the interface method calls. There I collect the thrown exceptions to then throw them further as composite exception (must process all Foos first.