jeudi 24 mars 2016

How to avoid many 'if's' and provide runtime params on execution

I am trying to avoid lots of if's and use an OOP way to execute different executions. the execution is determined by ACTION integer which I get from an API on runtime.

I though about command pattern and this is how it looks:

I created on Spring configuration a hashmap:

      @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 on some other place on my case I am injecting hashmapCommands:

on 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 params inside execution() it's wrong to use that using Command pattern.

As you can see I must send runtime message and avoiding if statements which retrieve the command execution from hashmap. But i need to sent param which I am getting only on runtime.

Mybe i am forcing Command pattern on my case? other suggestions ?

Aucun commentaire:

Enregistrer un commentaire