My problem is that you have a lot of ifconditions to be checked and inside that if condition you have multiple lines of code. I want to eliminate it and use inheritance instead. For every if statement I will be using a separate class to handle it. This is more like a derivative of the command pattern but not command pattern itself.I have an interface which contains only one method which defines the task I want it to be implemented. I have an abstract class implementing that interface and contains a static map and static methods to add values and retrieves the task. The classes I specified will be extending this Handler class.This is my scenario: I am sending a string as parameter to get a specific translator. Let's say Sinhala or Tamil or English. On each implementation of those translators, I will be adding the necessary values to the map in the Handler class in a static block. Basically adding them in the class load time.Command Interfacepublic interface Command {    LanguageTranslator getLangTranslator();}Handler Abstract classpublic abstract class Handler implements Command {    private static Map<String, Command> map = new HashMap<>();    public static void AddToMap(String key, Command command){        map.put(key,command);    }    public static LanguageTranslator getTranslator(String value) throws Exception {        if (!(map.containsKey(value))){            throw new LanguageNotFoundException(Translator not found);        }else {            return map.get(value).getLangTranslator();        }    } }Sinhala Translator public class SiCommand extends Handler {    static {        Handler.AddToMap(1,new SiCommand());    }    @Override    public LanguageTranslator getLangTranslator() {        return new SiTranslator();    }}Tamil Translatorpublic class TiCommand extends Handler {    static {        Handler.AddToMap(2,new TiCommand());    }    @Override    public LanguageTranslator getLangTranslator() {        return new TiTranslator();    }}English Translator public class EnCommand extends Handler {    static {        Handler.AddToMap(3,new EnCommand());    }@Override    public LanguageTranslator getLangTranslator() {        return new EnTranslator();    }}Demo  RowTranslator rowTranslator = Handler.getTranslator(2);QuestionsSince all values are assigned to the map during the class loading time these are my questions:Is using static blocks like this a good practice?I want to eliminate the use of if conditions as I mentioned before. By using this method, whenever someone wants to add a new if condition, you just only have to create a new class extending handler and implement it. Therefore the outside user doesn't need to know the internal handling of the Command's subclasses. The output is just gonna be a translator. Is this a good way of doing it? What are the better alternatives? What should be adjusted?Note: I will also be focusing strictly on Open/Close principle as well.
Multilingual command handler using inheritance
java;object oriented;design patterns;inheritance;i18n