Dynamic method dispatch

前端 未结 2 1050
春和景丽
春和景丽 2021-01-24 17:10

There is lot of info on Dynamic dispatch in the internet, I feel like a chicken as I am not able to implement it. Please help me. Here is what I am trying to do.



        
相关标签:
2条回答
  • 2021-01-24 17:33

    Try loading ClassC with reflection. If that attempt fails, your ClassC does not exist so the validation is OK by default. If ClassC exists, try finding the proper method and calling it with reflection too. If you don't find the method, then the validation is again OK by default (I guess). If you find the method, and the call to it returns true, then you're done, validation is OK. And if this call returns false, the validation failed.

    Check these links, and I think you should get what I am saying.

    http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName%28java.lang.String%29

    http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29

    http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html#invoke%28java.lang.Object,%20java.lang.Object...%29

    0 讨论(0)
  • 2021-01-24 17:34

    You could create a Dispatcher interface which simply defines a method dispatch(String) (or whatever you try to achieve). The base class (ClassB) uses a NullPattern which implements the interface while the child class (ClassC) implements the interface according your needs.

    The interface is quite simple:

    public interface Dispatcher
    {
        public void dispatch(String message);
    }
    

    The NullPattern is implemented like this:

    public class NullDispatcher implements Dispatcher
    {
        public void dispatch(String message)
        {
            // do nothing
        }
    }
    

    ClassB should be modified like this:

    public class ClassB
    {
        private Dispatcher dispatcher;
    
        public ClassB()
        {
            dispatcher = new NullDispatcher();
        }
    
        public void setDispatcher(Dispatcher dispatcher)
        {
            // change this to your needs
            if (dispatcher == null)
                dispatcher = new NullDispatcher();
            else
                this.dispatcher = dispatcher;
        }
    
        @Test
        public void myTest()
        {
            ClassA a = new ClassA();
            a.createRequest();
            String test = a.getResponse();
    
            dispatcher.dispatch(test);
        }
    }
    

    Here a new Dispatcher can be set using the setDispatcher(Dispatcher) method. This dispatcher will be used in myTest to dispatch the result of a.getResponse().

    The extending class just needs to set a specific implementation of the Dispatcher. F.e. to print the response to the console you could implement a ConsoleDispatcher like this:

    public class ConsoleDispatcher implements Dispatcher
    {
        public void dispatch(String message)
        {
            System.out.println(message);
        }
    }
    

    To use the ConsoleDispatcher instead of the NullDispatcher in ClassC you might use a code similar to:

    public class ClassC extends ClassB
    {
        public ClassC()
        {
            this.setDispatcher(new ConsoleDispatcher());
        }
    }
    

    As ClassC extends ClassB you will have access to myTest which uses the defined dispatcher to dispatch the message accordingly.

    HTH

    0 讨论(0)
提交回复
热议问题