1

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

for example:

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 ?

Thanks.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am using Spring for this. and I am creating those command entities in advanced. I cant provide those runtimes params in advanced only on execution time.
The thing is that I have a map of key object store that by action-key I know which command to execute. i am getting the action key on the invoker class. still try to understand my way how to glue all of that
I mean i am creating those objects on other place when initiliaizing my app. than based on action I know which one to use. and on usage-time(runtime) i need to pass it params that will be executed on the command objects
I think you've fallen into an XY problem. My recommendation is to spend some time thinking about what you're really trying to solve and then either update this question or ask a new one.
u right. please check my other question at: stackoverflow.com/questions/36194878/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.