通用返回类型 - Java

发布于 2024-10-17 18:27:12 字数 431 浏览 5 评论 0原文

我的问题是基于使用命令模式调用服务器方法。然而,有许多可能的命令。我想为每个命令实例的“执行”方法引入一个返回类型。我知道这不是命令模式的预期用途,但我别无选择。我需要定制适合我的命令模式。

对于每个命令中“执行”方法的方法签名,我可能有什么作为返回类型?我猜它必须是协变返回类型。这不是一个理想的解决方案,但我没有太多其他选择。我正在为我的 Android 应用程序开发服务器,但 Android SDK 中不提供 RMI。对于有关返回类型问题的任何建议,我将不胜感激。我需要考虑可以从所有不同命令返回的所有返回类型。我不确定是否存在针对返回某种通用返回类型的问题的模式。

我已经看过这个线程: 命令模式返回状态 但我需要更多灵感。

My question is based around invoking a server method by using the command pattern. However there are many possible commands. I would like to introduce a return type for the 'execute' method of each command instance. I know this is not the intended use of the command pattern, but I haven't any other choice. I need to tailor the command pattern to work for me.

For the method signature of the 'execute' method in each command, what could I possibly have as the return type? I'm guessing it would have to be a covariant return type. It's not an ideal solution but I haven't many other options. I'm developing a server for my android app and RMI isn't available in the Android SDK. I would appreciate any advice about the return type issue. I would need to take account of all of the return types that could be returned from all of the different commands. I'm not sure if there is a pattern out there for this issue of returning some sort of generic return type.

I have already looked at this thread:
command pattern returning status
but I need more inspiration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-10-24 18:27:12

我可能不明白这里真正的问题,但这听起来很简单。

public interface Command<T> {
  T execute();
}

然后命令可能是:

public class FooCommand implements Command<Bar> {

  public Bar execute() {
    ...
  }
}

或者有什么地方有问题吗?

I probably don't understand the real question here, but this sounds straightforward.

public interface Command<T> {
  T execute();
}

And then teh commands could be:

public class FooCommand implements Command<Bar> {

  public Bar execute() {
    ...
  }
}

Or is there a catch somewhere?

彩虹直至黑白 2024-10-24 18:27:12

看来您真正想要的是 RMI,而 Android 不支持它。

您可以尝试使用轻量级的RMI。

要自己实现一些东西,请从biziclop的界面开始,

// suppose there are object input/output streams established

// on client side
FooCommand foo = new FooCommand(params..);
Bar bar = remoteExec( foo );

<T> T remoteExec(Command<T> cmd)
     output.writeObject(cmd);
     return (T)input.readObject();

// on server side

    Command cmd = (Command)input.readObject();
    Object result = cmd.execute();
    output.writeObject(result); 

It looks like what you really want is RMI, which Android doesn't support.

You can try to use a lightweight RMI.

To implement something yourself, start from biziclop's interface,

// suppose there are object input/output streams established

// on client side
FooCommand foo = new FooCommand(params..);
Bar bar = remoteExec( foo );

<T> T remoteExec(Command<T> cmd)
     output.writeObject(cmd);
     return (T)input.readObject();

// on server side

    Command cmd = (Command)input.readObject();
    Object result = cmd.execute();
    output.writeObject(result); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文