I have a bunch of classes with a unique set of methods.
class1 {
method1(dbconn, args...);
method2(dbconn, args...);
}
class2 {
method3(dbconn, args...);
method4(dbconn, args...);
}
class3 {
method5(dbconn, args...);
}
We need to expose all the methods via a flat library:
class library {
init () {
//create instance of all helper classes
}
method1(args...) {
return class1Instance.method1(getDbconn(), args...);
}
method2(args...) {
return class1Instance.method2(getDbconn(), args...);
}
method3(args...) {
return class2Instance.method3(getDbconn(), args...);
}
method4(args...) {
return class2Instance.method4(getDbconn(), args...);
}
method5(args...) {
return class3Instance.method5(getDbconn(), args...);
}
}
But it is very time consuming, and there is a lot of repetitive code to move each method into the library. Is there a better way to do this?
Note that each method name is unique. The arguments and return value are of different types.
define an interface:
then use dynamic proxy to reflectively invoke methods of your service implementations. you will also have to find a solution on how to choose on which service particular method should be invoked. but this is not very hard.
you can implement a class: