Command line against reflection

I have a controller that executes some commands according to the command name taken from url. The main thing is not to use if and switch clauses. As I know, there are ONLY two ways to do this: 1) command template 2) reflection.

//Command pattern class Controller{ private HashMap<String,Command> commands; public void executeCommand(String commandName){ commands.get(commandName).execute(); } ... } //reflection class Controller{ public void readCommand(){ .... } public void executeCommand(String commandName){ this.getClass().getMethod(commandName+"Command").invoke(this); } ... } 

So, quests:

  • Which one is better?
  • This is normal in a single application to allow developers to use one of the methods they want.
  • Are there any other ways?
+5
source share
3 answers
  • The first way is better, use reflections only when you have no other options.
  • in one application there should be one approach to solving one problem.
  • I think the first approach is great. (much better if / else blocks)
+2
source

Which one is better?

Obviously, first is better. Although you indicated that you are using the Command pattern, it does not fill out the Command pattern. The team template will have team (abstract), concrete teams, receiver, interrogator and client.

Take a look at this question:

Using the command design pattern

Besides Command Patten, I would like to highlight the pros and cons of reflection.

Pros:

  • Dependency Injection Handling
  • Development of plug and play frameworks

Minuses:

  • Reflection challenge slower
  • You could violate security and explode the application with bad intent (for example, set private variables of a class that is invisible to another class)

Have a look at the relevant SE question regarding Reflection:

What is reflection and why is it useful?

This is normal in a single application to allow developers to use one of the methods they want.

Typically, developers choose the best method to solve a particular problem.

Are there other ways?

It depends on the type of problem you are about to solve. Design patterns provide solutions for recurring problems.

All solutions cannot match existing design patterns. You may have developed new templates to solve your problem.

+1
source

I think there are two different ways for your first approach. Each command can be a subclass of the abstract class Command. Or each command can be an instance of a class command. It depends on how flexible everything should be, and are the parameters and return values ​​for the commands? With subclasses, it will look like this (just to get an idea):

 abstract public class Command { abstract public void execute(); } public class LsCommand extends Command { @Override public void execute() { try { Runtime.getRuntime().exec("ls"); } catch (IOException e) {} } } public class ChdirCommand extends Command { @Override public void execute() { try { Runtime.getRuntime().exec("chdir"); } catch (IOException e) {} } } 

Here are my answers:

  • Your first way is better. Always prefer reflection design templates.
  • Sorry, I do not understand question 2. But in any case, he does not have a question mark, so I just skip it :)
  • You might want to look into the Strategy design template, where each team can be executed from different parts of the subcommands. Another idea is a Factory design template . In this case, you will put each command in a class, and then use ClassLoader to load the class by name.
0
source

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


All Articles