Dynamic method dispatch

不想你离开。 提交于 2019-12-20 05:13:19

问题


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.

ClassA{

    public void createReq(){
    }

    public String postReq(){
    }

}

ClassB{

@Test
public void myTest(){
Class A = new ClassA();
a.createReq();
String test = a.getResponse();

/* Not sure how do i do this part */

}

So, I get a string 'test' in myTest method. I want to create a ClassC that extends ClassB and write a method that would verify the string returned in myTest soon after the step (a.getResponse()).

If there is no ClassC implemented, I would just want to simply end the test. If only ClassC exists and implements a method for verification, I want the verification to be done.

How do I do this? Please help. Thanks.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/20857658/dynamic-method-dispatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!