策略模式-实现方式三

一 枚举类

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);
    }



}

源代码

相关推荐
cike_y6 分钟前
Java反序列化漏洞-Shiro721流程分析
java·反序列化·shiro框架
极创信息28 分钟前
信创系统认证服务怎么做?从适配到验收全流程指南
java·大数据·运维·tomcat·健康医疗
格鸰爱童话34 分钟前
向AI学习项目技能(六)
java·人工智能·spring boot·python·学习
白宇横流学长1 小时前
停车场管理系统的设计与实现
java
Flittly1 小时前
【SpringAIAlibaba新手村系列】(18)Agent 智能体与今日菜单应用
java·spring boot·agent
木井巳1 小时前
【递归算法】目标和
java·算法·leetcode·决策树·深度优先
亦暖筑序1 小时前
手写 Spring AI Agent:让大模型自主规划任务,ReAct 模式全流程拆解
java·人工智能·spring
敖正炀1 小时前
ReentrantLock 与 synchronized对比
java
XiYang-DING1 小时前
【Java】二叉搜索树(BST)
java·开发语言·python
weixin_437957611 小时前
Mysql安装不成功
java