I need to create a base class that implements several interfaces with lots of methods, example below.
Is there an easier way to delegate these method calls without having to create a horde of duplicate methods?
public class MultipleInterfaces implements InterFaceOne, InterFaceTwo {
private InterFaceOne if1;
private InterFaceTwo if2;
public MultipleInterfaces() {
if1 = new ImplementingClassOne();
if2 = new ImplementingClassTwo();
}
@Override
public void classOneMethodOne { if1.methodOne(); }
@Override
public void classOneMethodTwo { if1.methodTwo(); }
/** Etc. */
@Override
public void classTwoMethodOne { if2.methodOne(); }
@Override
public void classTwoMethodTwo { if2.methodTwo(); }
/** Etc. */
}
As said, there's no way. However, a bit decent IDE can autogenerate delegate methods. For example Eclipse can do. First setup a template:
public class MultipleInterfaces implements InterFaceOne, InterFaceTwo {
private InterFaceOne if1;
private InterFaceTwo if2;
}
then rightclick, choose Source > Generate Delegate Methods and tick the both if1
and if2
fields and click OK.
See also the following screens: