IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

策略模式+元注解方式替代大量if else写法

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1、策略模式简介

设计模式的知识可以参考我的设计模式笔记专栏:设计模式系列博客

策略模式:定义一系列算法,然后将每一个算法封装起来,并将它们可以互相替换。也就是将一系列算法封装到一系列策略类里面。策略模式是一种对象行为型模式。策略模式符合“开闭原则“

Strategy Pattern: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

策略模式包括如下角色:

  • Context :环境类
  • Strategy:抽象策略类
  • ConcreteStrategy:具体策略类

    策略模式和状态模式常用于处理业务比较繁杂的场景,因为业务经常变更,有时候随着业务堆积,会出现大量的if…else,造成代码可读性变差,所以可以使用策略模式和状态模式等设计模式进行业务解耦,提高代码可读性

2、典型例子实现

业务场景:提供一个统一的页面,嵌套各个子系统,点击各个子系统时候,会进行业务处理,然后进行跳转

业务听起来很简单,所以就简单敲下代码:

 public ModelAndView toSysPage(@RequestParam("type")String type, HttpServletRequest request){
        String viewName = "login/unifyLogin";
        String isCaLogin = request.getParameter(IS_CA_LOGIN);
        if (!StringUtils.isEmpty(isCaLogin) && "true".equalsIgnoreCase(isCaLogin)) {
            if (SysTypeEnum.SYS_APPR_CONTROL.getType().equals(type) ) {
                viewName = "login/yzsCA";
            } else if(SysTypeEnum.SYS_APPR_UNION_CONTROL.getType().equals(type) ) {
                viewName = "login/ydblCA";
            } else if(SysTypeEnum.SYS_APPR_UNIFY_WEB.getType().equals(type) ) {
                viewName = "login/jsgcCA";
            }
        }

        if (SysTypeEnum.SYS_APPR_CONTROL.getType().equals(type) && (StringUtils.isEmpty(isCaLogin) || !"true".equals(isCaLogin))) {
            viewName = "login/yzsLogin";
        } else if(SysTypeEnum.SYS_APPR_UNION_CONTROL.getType().equals(type)  && (StringUtils.isEmpty(isCaLogin) || !"true".equals(isCaLogin))) {
            viewName = "login/ydblLogin";
        } else if(SysTypeEnum.SYS_APPR_UNIFY_WEB.getType().equals(type)  && (StringUtils.isEmpty(isCaLogin) || !"true".equals(isCaLogin))) {
            viewName = "login/jsgcLogin";
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

然后,和现场沟通,发现还要增加系统,业务也要增加,所以就要增加if…else的数量,业务一堆积,代码就变得很杂,不好维护,所以用策略模式进行改进

  • 定义元注解:

import org.springframework.stereotype.Service; import java.lang.annotation.*; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited //子类可以继承此注解 public @interface SysType { String type(); }
  • 写个策略接口
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;

public interface SysHandler {
    ModelAndView invokeModelAndView(Map<String,Object> params);
}

  • 各个系统都实现接口,进行不同的业务处理,@SysType(type = "sys1")表示系统type,@Component记得加上,才可以加到Spring容器里
@SysType(type = "sys1")
@Component
public class ApprControlSysHandler implements SysHandler{

    @Override
    public ModelAndView invokeModelAndView(Map<String,Object> params) {
        //...
        return modelAndView;
    }
}


  • 在一个@Service类里,将实现SysHandler接口的类都装载到Spring容器
public static Map<String, SysHandler> sysHandlerMap = new HashMap<String, SysHandler>(16);

    @Autowired
    ApplicationContext applicationContext;

    /**
     * 装载到Spring容器
     * @Author nicky
     * @Date 2020/06/23 17:47
     * @Param [applicationContext]
     * @return void
     */
    @PostConstruct
    public void buildSysHandlerMap() {
        Map<String, Object>  map = applicationContext
                .getBeansWithAnnotation(SysType.class);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Class<SysHandler> sysHandlerClass = (Class<SysHandler>)entry.getValue().getClass()  ;
            String type = sysHandlerClass.getAnnotation(SysType.class).type();
            sysHandlerMap.put(type,applicationContext.getBean(sysHandlerClass));
        }
    }


  • 调用,进行改造,代码简洁很多
public ModelAndView toSysPage(String type, HttpServletRequest request){
        Assert.notNull(type, "type can not null");
          SysHandler sysHandler = sysHandlerMap.get(type);
          Map<String, Object> params = new HashMap<String, Object>(16);
          params.put("isCaLogin", isCaLogin);
          params = Collections.unmodifiableMap(params);
          return modelAndView = sysHandler.invokeModelAndView(params);
    }

看了类图,也很清晰,这是策略模式的简单应用,有什么问题欢迎指出
71_1.png

文章永久链接:https://tech.souyunku.com/?p=25393


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(65) 打赏



未经允许不得转载:搜云库技术团队 » 策略模式+元注解方式替代大量if else写法

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367