【Java 设计模式】策略模式,Spring实现方式

项目中的一个页面跳转功能存在10个以上的if else判断,想要做一下整改  (代码参照于蚂蚁课堂)

在spring项目中,无需再使用上文中的工厂方法获取具体策略的实现类

目录

controller层

service层

枚举类

utils:

 简单结构 

运行效果:


具体代码实现:

controller层

scanBasePackages配置解决idea 多module下的service类注入问题

这里用的是Class.forName("类路径")

亦可使用beanId的方式 SpringUtils.getBean(beanId)

package com.google.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author wk
 * @Description:
 * @date 2019/11/25 16:05
 **/
@SpringBootApplication(scanBasePackages="com.google")
public class WebApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}
package com.google.controller;

import com.google.fact.FactApplyStrategy;
import com.google.fact.PurposeEnum;
import com.google.util.SpringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wk
 * @Description:
 * @date 2019/12/26 17:59
 **/
@RestController
public class FactController {

    @RequestMapping(value = "/toAddFact")
    public String toAddFact(Integer code) {

        FactApplyStrategy factApplyStrategy = null;
        try {
            factApplyStrategy = (FactApplyStrategy)
                    SpringUtils.getBean(Class.forName(PurposeEnum.getEnumObjByCode(code).getClassName()));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return factApplyStrategy.toAddFact();
    }
}

service层

package com.google.fact;

/**
 * 共同行为
 */
public interface FactApplyStrategy {

    String toAddFact();
}
package com.google.fact.impl;

import com.google.fact.FactApplyStrategy;
import org.springframework.stereotype.Component;

/**
 * @author wk
 * @Description:
 * @date 2019/12/24 13:59
 **/
@Component
public class GdFactApplyStrategy implements FactApplyStrategy {

    @Override
    public String toAddFact() {
        return "跳转光大银行";
    }
}

package com.google.fact.impl;

import com.google.fact.FactApplyStrategy;
import org.springframework.stereotype.Component;

/**
 * @author wk
 * @Description:
 * @date 2019/12/26 14:51
 **/
@Component
public class JTFactApplyStrategy implements FactApplyStrategy {

    @Override
    public String toAddFact() {
        return "跳转交通银行";
    }
}

package com.google.fact.impl;

import com.google.fact.FactApplyStrategy;
import org.springframework.stereotype.Component;

/**
 * @author wk
 * @Description:
 * @date 2019/12/26 17:10
 **/
@Component
public class ZGFactApplyStrategy implements FactApplyStrategy {

    @Override
    public String toAddFact() {
        return "跳转中国银行";
    }
}

枚举类

package com.google.fact;

/**
 * 策略枚举类
 */
public enum PurposeEnum {

    TYPE_1(1, "中国银行", "com.google.fact.impl.ZGFactApplyStrategy"),

    TYPE_2(2, "光大银行", "com.google.fact.impl.GdFactApplyStrategy"),

    TYPE_3(3, "交通银行", "com.google.fact.impl.JTFactApplyStrategy"),
    ;

    private Integer code;
    private String mark;
    private String className;

    private PurposeEnum(Integer code, String mark, String className) {
        this.code = code;
        this.mark = mark;
        this.className = className;
    }

    public Integer getCode() {
        return code;
    }

    public String getMark() {
        return mark;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public static PurposeEnum getEnumObjByCode(Integer code) {
        for (PurposeEnum be : values()) {
            if (be.getCode().equals(code)) {
                return be;
            }
        }
        return null;
    }
}

utils:

package com.google.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author wk
 * @Description:
 * @date 2019/12/26 19:11
 **/
@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取Bean
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 泛型怎么是写在前面
     * 通过class获取Bean
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name class返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }


}

简单结构 

运行效果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值