我具有使用命令模式封装在diff命令上的功能.
I have functionality that I am encapsulate on diff commands using Command pattern.
我正在用信息和逻辑创建命令,它需要我如何获取一些参数仅在运行时,我需要提供命令
I am creating the command with the information and logic it need how ever I am getting some params only on runtime which I need to provide my commands
例如:
public class sendMessageToServerCommand implements Command { @Override public void execute(String msg){ sendToServerTheMsg(msg); } } .. Command command=new sendMessageToServerCommand(); command.execute("msg I got on runtime");也许我不应该使用命令模式并考虑其他事情?建议?
Perhaps I shouldnt use command pattern and think about something else? suggestions ?
谢谢.
推荐答案命令模式规定了一个对象,该对象在创建后就可以不带任何参数地执行(例如: Runnable 或 Callable ),但没有阻止创建过程中传递参数的措施.因此您只需将msg参数从execute()方法移至命令的构造函数即可.
The Command pattern stipulates an object that can be executed with no arguments after its creation (for example: Runnable or Callable) however, there is nothing preventing arguments from being passed during creation; so you can simply move the msg argument from the execute() method to the command's constructor.
在命令"模式的典型用法中,命令在一个位置创建并在另一位置执行.创建逻辑已参数化;执行逻辑不是.
In a typical use of the Command pattern, commands are created in one place and executed in another. The creation logic is parameterized; the execution logic is not.
更多推荐
如何通过传递给运行时参数来使用命令模式
发布评论