如何通过传递给运行时参数来使用命令模式 [英] How to use Command pattern by passing to it runtime params

查看:178
本文介绍了如何通过传递给运行时参数来使用命令模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有使用命令模式封装在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 ?

谢谢.

推荐答案

命令模式规定了一个对象,该对象在创建后就可以不带任何参数地执行(例如:

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.

这篇关于如何通过传递给运行时参数来使用命令模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆