Is there Any Alternative for Switch Case Statements in Java ? Any Good Design Pattern ? My Switch case Statement Increasing How to Avoid

你离开我真会死。 提交于 2021-02-08 10:30:52

问题


Below Code, I wrote to execute the switch-case statement, for each condition. Now I am thinking to remove the switch-case statement and apply some design pattern to overcome this issue. // main iteration - for every sheet for (Entry>> entry : testCaseSheetsDataMap.entrySet()) {

            String sheetNameKey = entry.getKey().trim();
            String testCaseName = testCaseSheetMasterMap.get(sheetNameKey).trim();
            List<Map<String, Object>> executableRowsList = new ArrayList<Map<String, Object>>();
            executableRowsList = entry.getValue();

            CitiMainAuxiliary auxiliary = new CitiMainAuxiliary();

            switch (testCaseName) {

            case "Anonymous Mode Log In":
                auxiliary.runAllLogin(executableRowsList, testCaseName, Constants.ANONYMOUS);
                break;
            case "Login Mode":
                auxiliary.runAllLogin(executableRowsList, testCaseName, Constants.LOGIN);
                break;
            case "Cookied Mode Login":
                auxiliary.runAllLogin(executableRowsList, testCaseName, Constants.COOKIED);
                break;
            case "OBO Mode Login":
                auxiliary.runAllLogin(executableRowsList, testCaseName, Constants.OBO);
                break;

            case "Anonymous Mode Megamenu":
                auxiliary.runMegaMenu(executableRowsList, testCaseName, Constants.ANONYMOUS);
                break;
            case "Login Mode Megamenu":
                auxiliary.runMegaMenu(executableRowsList, testCaseName, Constants.LOGIN);
                break;
            case "Cookied Mode Logon - Megamenu Check":
                auxiliary.runMegaMenu(executableRowsList, testCaseName, Constants.COOKIED);
                break;
            case "OBO Logon - Megamenu Check":
                auxiliary.runMegaMenu(executableRowsList, testCaseName, Constants.OBO);
                break;

            }

        } // end-for testCaseSheetsDataMap

回答1:


In your case you can write a HashMap<string,Constants> and run something like:

auxiliary.runMegaMenu(executableRowsList, testCaseName, map.get("Login Mode Megamenu"));

and additionally you could introduce a HashSet<string>() where you store all strings for the runMegaMenu-methods and than you could check it the following way:

if(set.contains(testCaseName)){
   auxiliary.runMegaMenu(executableRowsList, testCaseName, map.get(testCaseName));
}else{
   auxiliary.runAllLogin(executableRowsList, testCaseName, map.get(testCaseName));
}

But back to your question. Switch-case can generally be avoided by polymorphism I personally prefer the Strategy-Pattern for cases like your example.




回答2:


As was mention you can use polymorphism and Strategy pattern.

For example you can create Map<String, BiConsumer<CitiMainAuxiliary, List<Map<String, Object>>> strategies and populate it

strategies.put("Anonymous Mode Log In", (aux, list)-> aux.runAllLogin(list,...))

After that instead of switch statement you can use strategy = strategies.get(testCaseName); strategy.accept(...)

If you can't use Java 8 you can create an Interface and use Map<String, YourInterface> instead of map of BiConsumer. Maybe the implementation with interfaces is better, because in every particular class you can initialize your Constants, like COOKIED or OBO



来源:https://stackoverflow.com/questions/47172767/is-there-any-alternative-for-switch-case-statements-in-java-any-good-design-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!