开发者

pattern to proxy (different) methods to many different classes

开发者 https://www.devze.com 2023-04-12 15:18 出处:网络
I have a bunch of classes with a unique set of methods. class1 { method1(dbconn, args...); method2(dbconn, args...);

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(a开发者_C百科rgs...) {
        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:

interface library {
    method1();
    ...
    methodN();
}

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:

class ServiceDemultiplexer implements InvocationHandler {

     ServiceDemultiplexer(Object services[]) {
       ... 
     }

     public Object invoke(Object proxy, Method m, Object[] args)
    throws Throwable
     {
        ...
     }
}


The better way of doing it it to avoid doing it.

A class should have a well-defined, as coherent as possible, set of responsibilities. If all these classes exist, it's certainly because each one has a different set of responsibilities than the other ones. Why don't you let the caller choose the appropriate class to use and call the appropriate method from this class.

You could use your Library class as a factory of helper classes. The callers would just have to do

library.getClass1().method1(...);

If you guive it real names, it becomes more natural:

library.getProductManager().createProduct();

But even then, injecting the specific dependencies to caller object looks better to me than relying on a huge factory like this. Use a dependency injection framework like Spring or Guice.


Depending on what you are working on and if using reflection is not an issue, you might call everything through a dynamic proxy.

http://www.javaworld.com/javaworld/jw-11-2000/jw-1110-proxy.html

Your InvocationHandler.invoke would just locate class and method declaring Method m in its parameter, instantiate an object of that class (could be done sooner than that too) and pass the call to it. The actual implementation depends on what you already have.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号