Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Pattern - parameters

I'm looking to use the command pattern in a distributed client/server environment. Essentially, the receivers 'execute' methods will need to take various parameters, however I read that each command class should have a uniform 'execute' method that should reveal nothing about the underlying functionality of the receivers.

My question is, how can I pass invocation parameters from switches into the different receivers via the command classes? Has anyone got a simple Java example? I can't seem to find any

Thanks indeed for your help.

like image 407
Joeblackdev Avatar asked Sep 11 '26 23:09

Joeblackdev


1 Answers

Just pass them upon construction of the command instance.

public class ConcreteCommand implements Command {

    private Object something;

    public ConcreteCommand(Object something) {
        this.something = something;
    }

    @Override
    public void execute() {
        // ...
    }

}

Or if you really need to pass arguments (because they represent the work state and not the algorithm state), then you should just do so and call it "strategy pattern" instead ;)

like image 66
BalusC Avatar answered Sep 14 '26 12:09

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!