策略模式-实现方式三

一 枚举类

csharp 复制代码
@Getter
public enum AuthTypeEnum {

    QCT_PASSWORD("qct_password", "密码"),
    MOBILE("mobile", "验证码");
    public final String code;
    public final String desc;

    AuthTypeEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static AuthTypeEnum getByCode(String codeVal) {
        for (AuthTypeEnum resultCodeEnum : AuthTypeEnum.values()) {
            if (resultCodeEnum.code.equals(codeVal)) {
                return resultCodeEnum;
            }
        }
        return null;
    }
}

二 抽象策略类

csharp 复制代码
public abstract class AbstractLoginService {
    public static String login( String authType) {
        Map<String, AbstractLoginService> LoginServices = ApplicationConfiguration.getBeansOfType(AbstractLoginService.class);
        for (AbstractLoginService loginService : LoginServices.values()) {
            if (loginService.getType().equals(authType)) {
                loginService.doLogin(authType);
                return loginService.loginFinal(authType);
            }
        }
        throw new RuntimeException("无效的登录方式");
    }


    public abstract String getType();

    protected abstract void doLogin(String authType);

    protected abstract String loginFinal(String authType);
}

三 具体策略实现类

csharp 复制代码
@Slf4j
@Service
public class CaptchaLoginService extends AbstractLoginService {

    @Override
    public String getType() {
        return AuthTypeEnum.MOBILE.getCode();
    }

    @Override
    protected void doLogin(String authType) {
      log.info("验证码登录");
    }



    @Override
    protected String loginFinal(String authType) {
        return authType;
    }
}
csharp 复制代码
@Slf4j
@Component
public class PasswordLoginService extends AbstractLoginService {


    @Override
    public String getType() {
        return AuthTypeEnum.QCT_PASSWORD.getCode();
    }


    @Override
    protected void doLogin(String authType) {
        log.info("密码登录");
    }



    @Override
    protected String loginFinal(String authType) {
        return authType;
    }

}

四 应用上下文配置

csharp 复制代码
@Configuration
public class ApplicationConfiguration implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }

    public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
        return applicationContext.getBeansOfType(clazz);
    }

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

五单元测试

csharp 复制代码
@SpringBootTest
class LoginTest {


    @Test
    void loginPasswordTest() {
        String loginType = AuthTypeEnum.QCT_PASSWORD.code;
        String login = AbstractLoginService.login(loginType);
        System.out.println("login = " + login);
    }

    @Test
    void loginCaptchaTest() {
        String loginType = AuthTypeEnum.MOBILE.code;
        String login = AbstractLoginService.login(loginType);
        System.out.println("login = " + login);
    }



}

源代码

相关推荐
青山的青衫3 小时前
【前后缀】Leetcode hot 100
java·算法·leetcode
q***46523 小时前
基于SpringBoot和PostGIS的各省与地级市空间距离分析
java·spring boot·spring
狂团商城小师妹3 小时前
JAVA国际版同城服务同城信息同城任务发布平台APP源码Android + IOS
android·java·ios
后端小张3 小时前
【JAVA 进阶】Spring Boot 自动配置原理与自定义 Starter 实战
java·spring boot·后端·spring·spring cloud·自定义·原理
Zzzzmo_3 小时前
Java数据结构:二叉树
java·数据结构·算法
多多*3 小时前
一个有 IP 的服务端监听了某个端口,那么他的 TCP 最大链接数是多少
java·开发语言·网络·网络协议·tcp/ip·缓存·mybatis
Kay_Liang4 小时前
Spring IOC核心原理与实战技巧
java·开发语言·spring boot·spring·ioc·依赖注入·控制反转
LSL666_4 小时前
Spring 框架整合 JUnit 单元测试——包含完整执行流程
spring·junit·log4j
Mr.wangh4 小时前
单例模式&阻塞队列详解
java·开发语言·单例模式·多线程·阻塞队列
Slow菜鸟4 小时前
Java后端常用技术选型 |(三)分布式篇
java·分布式