如果在创建具有不同值的对象时出现条件,如何避免大量if else

时间:2017-03-08 20:40:47

标签: java enums refactoring

我有四个不同的枚举UserOneStatus,UserTwoStatus,UserThreeType和UserFourType,它们有ID和名称

我必须根据这些枚举ID过滤用户,所以我最终得到以下代码

if (ChoosenUserFilterValue.equals(String.valueOf(UserOneStatus.REQUESTED.getId()))) {
            selecedUserFilter = new UserFilter(UserOneStatus.class,
                    String.valueOf(UserOneStatus.REQUESTED.getId()),
                    UserOneStatus.REQUESTED.getUserStatus(), String.valueOf(UUID.randomUUID()));
        } else if (ChoosenUserFilterValue.equals(UserOneStatus.ACTIVE.getId() + "")) {
            selecedUserFilter = new UserFilter(UserOneStatus.class,
                    String.valueOf(UserOneStatus.ACTIVE.getId()),
                    UserOneStatus.ACTIVE.getUserStatus(), String.valueOf(UUID.randomUUID()));
        } else if (ChoosenUserFilterValue.equals(UserOneStatus.INACTIVE.getId() + "")) {
            selecedUserFilter = new UserFilter(UserOneStatus.class,
                    String.valueOf(UserOneStatus.INACTIVE.getId()),
                    UserOneStatus.INACTIVE.getUserStatus(), String.valueOf(UUID.randomUUID()));
        } else if (ChoosenUserFilterValue.equals(UserTwoStatus.REVOKED.getId() + "")) {
            selecedUserFilter = new UserFilter(UserTwoStatus.class,
                    String.valueOf(UserTwoStatus.REVOKED.getId()),
                    UserTwoStatus.REVOKED.getLoginStatus(), String.valueOf(UUID.randomUUID()));
        } else if (ChoosenUserFilterValue.equals(UserThreeType .ADMIN.getId() + "")) {
            selecedUserFilter = new UserFilter(Some.class,
                    String.valueOf(UserThreeType.ADMIN.getId()),
                    UserThreeType.ADMIN.getName(), String.valueOf(UUID.randomUUID()));
        } else if (ChoosenUserFilterValue.equals(UserFourType .SSO_TEMPLATE.getId() + "")) {
            selecedUserFilter = new UserFilter(Some.class,
                    String.valueOf(UserFourType.SSO_TEMPLATE.getId()),
                    UserFourType.TEMPLATE.getName(), String.valueOf(UUID.randomUUID()));
        }

我重构了一点,但我无法将结果与上面的代码相同

重构的代码如下

Map<String,Class<?>> map = new HashMap<String, Class<?>>();
map.put(UserOneStatus.REQUESTED.getId()+"", UserOneStatus.class);
map.put(UserOneStatus.ACTIVE.getId()+"", UserOneStatus.class);
map.put(UserOneStatus.INACTIVE.getId()+"", UserOneStatus.class);
map.put(UserTwoStatus.REVOKED.getId()+"", UserTwoStatus.class);
map.put(UserThreeType.ADMIN.getId()+"", Some.class);
map.put(UserFourType.TEMPLATE.getId()+"", Some.class);

String key ="";

if(map.containsKey(ChoosenUserFilterValue)){
    Class<?> getSelectedUserFilterValueClass = map.get(selectedUserFilterValue);

     for (Entry<String, Class<?>> entry : map.entrySet()) {
                key = entry.getKey();
        }

    selecedUserFilter = new UserFilter(getSelectedUserFilterValueClass, key , getSelectedUserFilterValueClass.toString(), String.valueOf(UUID.randomUUID());
}

任何建议表示赞赏

谢谢

1 个答案:

答案 0 :(得分:0)

基于某种输入数据(如id,数字等)创建一个接口/抽象类的对象的问题应该使用工厂方法设计模式或抽象工厂模式来解决。您的代码是一种工厂方法,在这种情况下使用if / else语句非常好。您可以在此处查看factory methodabstract factory的示例。