设计模式之策略模式

来源:互联网 发布:淘宝货款什么时候解冻 编辑:程序博客网 时间:2024/04/29 16:30

主要用作替代多层if else


背景为现在有多个应用需要导出,每个应用的导出逻辑都有所不同切连接不同数据库。所以需要对每个应用进行单独的逻辑处理。

而页面传入时需要区分是何种应用,根据应用id找到对应逻辑处理类。如果用if else判断则会出现几十个if else判断,经过查询资料,

决定使用策略模式。


配置文件:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <bean id="ExportFactory" class="io.renren.export.ExportFactory">        <property name="exportStrategyMap">            <map>                <entry key="87" value-ref="imgBookStrategy"/>                <entry key="91" value-ref="formulaimgStrategy"/>                <entry key="92" value-ref="mathgsStrategy"/>                <entry key="117" value-ref="enarticleStrategy"/>                <entry key="84" value-ref="enStrategy"/>                <entry key="127" value-ref="stuStrategy"/>                <entry key="62" value-ref="tstStrategy"/>                <entry key="68" value-ref="yymifStrategy"/>                <entry key="79" value-ref="mnmifStrategy"/>                <entry key="99" value-ref="gzmifStrategy"/>                <entry key="100" value-ref="hfmifStrategy"/>                <entry key="101" value-ref="csmifStrategy"/>                <entry key="102" value-ref="sxmifStrategy"/>            </map>        </property>    </bean>    <bean id="imgBookStrategy" class="io.renren.export.impl.ImgBookStrategy"></bean>    <bean id="formulaimgStrategy" class="io.renren.export.impl.FormulaimgStrategy"></bean>    <bean id="mathgsStrategy" class="io.renren.export.impl.MathgsStrategy"></bean>    <bean id="enarticleStrategy" class="io.renren.export.impl.EnarticleStrategy"></bean>    <bean id="enStrategy" class="io.renren.export.impl.EnStrategy"></bean>    <bean id="stuStrategy" class="io.renren.export.impl.StuStrategy"></bean>    <bean id="tstStrategy" class="io.renren.export.impl.TstStrategy"></bean>    <bean id="yymifStrategy" class="io.renren.export.impl.YymifStrategy"></bean>    <bean id="mnmifStrategy" class="io.renren.export.impl.MnmifStrategy"></bean>    <bean id="gzmifStrategy" class="io.renren.export.impl.GzmifStrategy"></bean>    <bean id="hfmifStrategy" class="io.renren.export.impl.HfmifStrategy"></bean>    <bean id="csmifStrategy" class="io.renren.export.impl.CsmifStrategy"></bean>    <bean id="sxmifStrategy" class="io.renren.export.impl.SxmifStrategy"></bean></beans>


工厂类:

public class ExportFactory {    private Map<String,ExportStrategy> exportStrategyMap = new HashMap<String, ExportStrategy>();    public Map<String, ExportStrategy> getExportStrategyMap() {        return exportStrategyMap;    }    public void setExportStrategyMap(Map<String, ExportStrategy> exportStrategyMap) {        this.exportStrategyMap = exportStrategyMap;    }    //执行方法,根据appid获取对应配置的class    public boolean executeStrategy(String tname,HttpServletRequest request,HttpServletResponse response){        String appId = request.getParameter("appid");        ExportStrategy exportStrategy = this.exportStrategyMap.get(appId);        if(exportStrategy == null){            return false;        }        return exportStrategy.doExport(tname,request);    }}


控制器:
当一个请求到服务端,调用工厂类获取bean,根据传入的应用id获取对应的key所对应逻辑实现。从而替代了多层if else

@RequestMapping("/export")@ResponseBodypublic boolean export(HttpServletRequest request, HttpServletResponse response){//获取任务名
String tname = sysAppsService.getTname(request.getParameter("taskId"));
ExportFactory factory = (ExportFactory) context.getBean("ExportFactory");return factory.executeStrategy(tname,request,response);}}

对应接口:

public interface ExportStrategy {     boolean doExport(String tname,HttpServletRequest request);}