开发者

Can I call make runtime decided method calls in Java?

I know there is an invoke function that does the stuff, I am overall interested in the "correctness" of using such a behavior. My issue is this: I have a Service Object witch contains methods which I consider services. What I want to do is alter the behavior of those services without later intrusion. For example:

class MyService {

      public ServiceResponse ServeMeDonuts() {
      do stuff...
      return new ServiceResponse();

}

after 2 months I find out that I need to offer the same service to a new client app and I also need to do certain extra stuff like setting a flag, or make or updating certain data, or encode the response differently. What I can do is pop it up and throw down some IFs. In my opinion this is not good as it means interaction with tested code and may result in un wanted behaviour for the previous service clients.

So I come and add something to my registry telling the system that the "NewClient" has a different behavior. So I'll do something like this:

public interface Behavior {
    public void preExecute();
    public void postExecute();
}

public class BehaviorOfMyService implements Behavior{
      String method;
      String clientType;

      public void BehaviorOfMyService(String method,String clientType) {
              this.method = method;
              this.clientType = clientType;
      }

      public void preExecute() {

        Method preCall = this.getClass().getMethod("pre" + this.method + this.clientType);
        if(preCall != null) {
            return preCall.invoke();
        } 
           return false;
      }

      ...same for postExecute();
     public void preServeMeDonutsNewClient() {
           do the stuff...
     }
}

when the system will do something like this

if(registrySaysThereIs different behavior set for this ServiceObject) {
      Class toBeCalled = Class.forName("BehaviorOf" + usedServiceObjectName);
      Object instance = toBeCalled.getConstructor().newInstance(method,client);
      instance.preExecute();
      ....call the service...
      instance.postExecute();
      ....
}

I am not particularly interested in correctness of code as in correctness of thinking and approach. Actually I have to do this in PHP, witch I see as a kind of Pop music of programming which I have to "sing" for commercial re开发者_如何学Pythonasons, even though I play POP I really want to sing by the book, so putting aside my more or less inspired analogy I really want to know your opinion on this matter for it's practical necessity and technical approach.

Thanks


Nice case to apply Aspect Oriented Programming. With AOP you can attach aspects to events or methods in order to process extra logic before or after the event.

There are good examples of AOP here and also examples for Java here

This example using aspectwerkz is very similar to your case, is basically adding post and pre method handlers around a service execution.

I'm not really familiar with AOP in PHP but there are some projects that you can use like this one.


In my opinion this is not good as it means interaction with tested code and may result in un wanted behaviour for the previous service clients.

That's why you have automated tests. What you want can be done with AOP, but really, really should not, because it will make the code very hard to maintain (which is a general problem of AOP and a reason why it hasn't caught on much).

It's just another example of making code worse for fear of breaking something that works. In the end, any change can potentially break things, and going through painful contortions to somehow isolate the changes from the "good" code and yet have them work as well will only end in misery.


What you need is a Proxy whereby you define some additional behaviour independently from the object being proxied. E.g:

public class ServiceHandler implements InvocationHandler {
    private final Object target;

    public static Object createProxy(Object target) {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), 
            target.getClass().getInterfaces(), new ServiceHandler(target));
    }

    public ServiceHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {
        // define preexecute behaviour
        Object result = method.invoke(target, args);
        // define postexecute behaviour
        return result;
    }
}

You would then use it as follows:

    public static void main(String[] args) {
        Service serviceImpl = new ServiceImpl();
        Service service = (Service) ServiceHandler.createProxy(serviceImpl);
        // use service
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜