jeudi 14 décembre 2017

Best Java design pattern to handle console commands

I'm trying to create a console to handle console commands from a string.

At the moment I'm using if statements to check for each command like this:

if (command.contains("new train")) {
    command = command.replace("new train ", "");
    Train t = new Train();
    t.setCode(command);
    ServiceProvider.getTrainService().saveOrUpdate(t);
    responeHandler("train " + command + " created");
}

But this isn't the best solution in my opinion.

I'm wondering if theres allready a good design pattern for problems like this?

I have looked at the builder and factory patterns but can't really decide if they are the right choice.

Aucun commentaire:

Enregistrer un commentaire