如何通过传递给运行时参数来使用命令模式

编程入门 行业动态 更新时间:2024-10-10 04:19:41
本文介绍了如何通过传递给运行时参数来使用命令模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何通过传递给运行时参数来使用命令模式

本文发布于:2023-11-23 22:18:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   参数   模式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!