How to simulate an increasing number of commands with parameters in pure form

This is a follow-up thread on How to get rid of instanceof in this Builder implementation

There are some more problems with this design. Each time a new parameter is entered, a new ConcereteParameter class must be created.

It's not a problem. But you also need to add a method to CommandBuilder append(ConcreteParameter). And I do not really like this addiction.

To summarize

  • Commands can be configured with options. Not every team can receive the same parameters. Therefore, some should be ignored. When applied to a team (in this implementation, this is achieved by throwingUnsupportedOperationException

  • Parameters that can be applied to specific classes are used differently in these classes (for example, FTPCommand and HTTPCommand can use IpParameter differently)

  • In the future, new commands and parameters may be introduced.

Upgrade Implementation, as now works. But isn’t it too difficult if I have about 30 parameters, what for each parameter should I have a separate method?

If there is, What is a cleaner and more flexible way / template to achieve this?

+1
source share
2 answers

? , , , . , , String Integer - , . -, : (, , ), .

, , . @Parameter, . . @Parameter void setIP(String) , String am IP-. , , , , . ​​, , , .

+1

, , , .

Map execute . Map String.

public interface Command {
   public void execute(Map<String, Object> context);
}

class OneCommandImpl extends Command {
    public void execute(Map<String, Object> context) {
        context.get('p1');
        context.get('p2');
    }
}

, , . , , . , .

0

All Articles