Avoiding many ifs and providing runtime options

I am trying to avoid a large number if'sand use the OOP method to perform various executions. execution is determined by the integer ACTION that I get from the API at runtime.

At least I’m talking about a team template, and here is how it looks:

I created a hash map in the Spring configuration:

      @Bean
        public HashMap<Integer, Command> hashmapCommands() {
            HashMap<Integer, Command> supportedCommands = new HashMap<>();
            supportedCommands.put(command.action.getId(), myCommand());
            ..
    }

  @Bean
    public Command MyCommand() {
        return new myCommand();
    }


public class MyCommand implements Command {

    public final static ACTION action = 1;
   @Override
    public void execute(String runTimeData) {
      //doSomeLogic
    }

}

Now, in some other place on my case, I insert hashmapCommands:

in inoker class:

public class TestCommands(){
..
 supportedCommands = (HashMap<Integer, Command>) context.getBean("hashmapCommands");
...

public void someTest(){
 Command myCommand = supportedCommands.get(1);
 reportCommandBase.execute("some runtimeMessage");
}

I have parameters inside the execution (), it is wrong to use using the Command pattern.

As you can see, I have to send a message at runtime and avoid statements that extract command execution from hashmap. But I need to send a parameter that I get only at runtime.

Mybe ? ?

+1

Source: https://habr.com/ru/post/1676767/


All Articles