命令模式 - 返回一个值
我使用命令模式通过 TCP/IP 套接字将命令从客户端传递到服务器。服务器将获取命令对象,将其反序列化,然后在命令对象上调用execute()。但是,我需要通过套接字将一个值传递回调用者。命令模式允许这样做吗?如果没有,有解决办法吗?我在维基百科上查看了灯开关示例,这很棒,但没有返回值。 任何建议都非常感激。
I'm using the command pattern for passing a command from a client to a server via a TCP/IP socket. The server will take the command object, deserialize it and then call execute() on the command object. However, I need to pass a value back to the caller over the socket. Does the command pattern allow for this? If not, is there a work around? I have looked at the light switch example on wikipedia, which is great, but there are no return values.
Any advice greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该在发送到远程服务器的命令上使用“execute()”方法,这在很多方面都是不好的,尤其是在 Java 中。
Command
应代表接收者应采取的操作。在本例中是调用某个对象的方法。Command
模式表示已采取或将采取的操作,而不是这些操作的实现。更多地考虑一组要执行的指令。您所描述的基本上是一种过度设计的 RPC 调用机制。不要重新发明这个轮子。看看现有的 RPC 机制,Java 世界中有很多可供选择的机制。然后您需要决定 RPC 是同步还是异步。
基于 REST 的 API 是什么作为一种 API,它很受欢迎,并且比任何本机语言特定机制(如 RMI)的寿命更长。
You should not have an "execute()" method on the
Command
sent to the remote server, this is bad in lots of ways, especially in Java. TheCommand
should represent the action the recipient should take. Which in this case is to call a method on some object.The
Command
Pattern is to represent actions taken or to be taken, not the implementation of those actions. Think more of a set of instructions to be carried out.What your are describing is basically an over-engineer RPC call mechanism. Don't re-invent this wheel. Looks at existing RPC mechanisms, there are plenty to choose from in the Java world. Then you need to decide if the RPC is synchronous or asynchronous.
A REST based API is what is popular and will last longer as an API than any native language specific mechanism like RMI.