dimanche 25 septembre 2016

Alternative for If conditions and replacing it with inheritance

So my problem is that you have a lot of if conditions 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.

Below is my code.

Command Interface

public interface Command {
    LanguageTranslator getLangTranslator();
}

Handler Abstract class

public 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 Translator

public 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");

Questions

Since All values are assigned to the map during the class loadding time these are my questions

  1. Is using static blocks like this a good practice ?

  2. I want to eliminate the use of if conditions as i mentioned before. By using this method when ever 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 sub classes. 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

Also I will be focusing strictly on Open/Close principle as well

Aucun commentaire:

Enregistrer un commentaire