Answer by Hoa Nguyen for Long list of if statements in Java
Command pattern is the way to go. Here is one example using java 8:1. Define the interface:public interface ExtensionHandler { boolean isMatched(String fileName); String handle(String fileName);}2....
View ArticleAnswer by Marlon Bernardes for Long list of if statements in Java
The answer provided by @dfa is the best solution, in my opinion. I'm just providing some snippets in case you are using Java 8 and want to use Lambdas! Command without parameters:Map<String,...
View ArticleAnswer by Blessed Geek for Long list of if statements in Java
Implementing an interface as demonstrated simply and plainly by dfa is clean and elegant (and "officially" supported way). This what the interface concept is meant for.In C#, we could use delegates for...
View ArticleAnswer by tinyd for Long list of if statements in Java
I'm not sure if you have any overlap between the behaviour of your various commands, but you might also want to take a look at the Chain Of Responsibility pattern which could provide more flexibility...
View ArticleAnswer by Rich Seller for Long list of if statements in Java
You're probably best off using a Map of Commands.But is you have a set of these to handle you end up with loads of Maps knocking about. Then it is worth looking at doing it with Enums.You can do it...
View ArticleAnswer by jens for Long list of if statements in Java
My suggestion would be a kind of lightweight combination of enum and Command object. This is an idiom recommended by Joshua Bloch in Item 30 of Effective Java.public enum Command{ A{public void...
View ArticleAnswer by Andreas Petersson for Long list of if statements in Java
i usually try to solve it that way:public enum Command {A {void exec() { doCommandA();}},B {void exec() { doCommandB();}};abstract void exec(); }this has many advantages:1) it is not possible to add an...
View ArticleAnswer by Pierre for Long list of if statements in Java
if you have multiple imbricated 'if' statements, then this is a pattern for using a rule engine. See, for example JBOSS Drools.
View ArticleAnswer by svachon for Long list of if statements in Java
Even if I believe the command pattern approach is more in toward best pratices and maintainable in the long run, here's a one liner option for...
View ArticleAnswer by user147042 for Long list of if statements in Java
if it was possible to have an array of procedures(what you called commands) that'd be useful..but you could write a program to write your code. It's all very systematicif(value='A') commandA();...
View ArticleAnswer by JeeBee for Long list of if statements in Java
Have an enum of commands:public enum Commands { A, B, C; }...Command command = Commands.valueOf(value);switch (command) { case A: doCommandA(); break; case B: doCommandB(); break; case C: doCommandC();...
View ArticleAnswer by ars for Long list of if statements in Java
Just use a HashMap, as described here:http://www.equivalence.co.uk/archives/99
View ArticleAnswer by keuleJ for Long list of if statements in Java
Well I suggest to create command objects and put them into a hashmap using the String as Key.
View ArticleAnswer by Mark for Long list of if statements in Java
If it does a lot of things, then there will be a lot of code, you cant really get away from that. Just make it easy to follow, give the variables very meaningful names, comments can help too...
View ArticleAnswer by dfa for Long list of if statements in Java
using Command pattern:public interface Command { void exec();}public class CommandA() implements Command { void exec() { // ... }}// etc etcthen build a Map<String,Command> object and populate it...
View ArticleLong list of if statements in Java
Sorry I can't find a question answering this, I'm almost certain someone else has raised it before. My problem is that I'm writing some system libraries to run embedded devices. I have commands which...
View Article
More Pages to Explore .....