动态方法调度 [英] Dynamic method dispatch
问题描述
互联网上有很多关于动态调度的信息,我感觉自己像个鸡,因为我无法实现它。请帮我。这是我想要做的。
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 */
}
所以,我在 myTest
方法中得到一个字符串'test'
。我想创建一个扩展 ClassB
的 ClassC
并编写一个方法来验证<$ c $中返回的字符串c> myTest 在步骤后不久( a.getResponse()
)。
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()
).
如果没有实现 ClassC
,我只想简单地结束测试。如果仅存在 ClassC
并实现验证方法,我希望验证完成。
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.
推荐答案
您可以创建一个 Dispatcher
界面,它只是定义了一个方法 dispatch(String)
(或者你试图实现的任何东西)。基类(ClassB)使用NullPattern实现接口,而子类(ClassC)根据您的需要实现接口。
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.
接口非常简单:
public interface Dispatcher
{
public void dispatch(String message);
}
NullPattern的实现如下:
The NullPattern is implemented like this:
public class NullDispatcher implements Dispatcher
{
public void dispatch(String message)
{
// do nothing
}
}
ClassB应该像这样修改:
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);
}
}
这里有一个新的 Dispatcher
可以使用 setDispatcher(Dispatcher)
方法设置。此调度程序将在 myTest
中用于调度 a.getResponse()
的结果。
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()
.
扩展类只需要设置 Dispatcher
的特定实现。 F.E.要打开对控制台的响应,您可以实现 ConsoleDispatcher
,如下所示:
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);
}
}
使用 ConsoleDispatcher
而不是 ClassC
中的 NullDispatcher
,您可以使用类似于的代码:
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());
}
}
由于ClassC扩展了ClassB,您将可以访问 myTest
使用定义的调度程序相应地调度消息。
As ClassC extends ClassB you will have access to myTest
which uses the defined dispatcher to dispatch the message accordingly.
HTH
这篇关于动态方法调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!