Using a strategy pattern and a command pattern

I’m including an encapsulation hierarchy table of several of the GoF design patterns to help explain the differences between these two patterns. Hopefully it better illustrates what each encapsulates so my explanation makes more sense. First off, the hierarchy lists the scope for which a given pattern is applicable, or the appropriate pattern to use … Read more

Using Command Design pattern

public interface Command { public void execute(); } For the most part, commands are immutable and contain instructions that encapsulate a single action that is executed on demand. You might also have a RuntimeCommand that accepts instructions upon execution, but this delves more into the Strategy or Decorator Patterns depending on the implementations. In my … Read more

Long list of if statements in Java

using Command pattern: public interface Command { void exec(); } public class CommandA() implements Command { void exec() { // … } } // etc etc then build a Map<String,Command> object and populate it with Command instances: commandMap.put(“A”, new CommandA()); commandMap.put(“B”, new CommandB()); then you can replace your if/else if chain with: commandMap.get(value).exec(); EDIT you … Read more