策略模式-实现方式三

一 枚举类

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



}

源代码

相关推荐
庞轩px6 分钟前
致远互联实习复盘:一条SQL替代300次循环查询,组织架构选择器从5秒降到300毫秒
java·sql·mysql·mybatis·实习经历·n+1问题·join联表查询
vooy pktc7 分钟前
Spring Security 官网文档学习
java·学习·spring
钰衡大师10 分钟前
Activiti 7 工作流技术文档
java·数据库·spring boot
dvjr cloi32 分钟前
Spring Framework 中文官方文档
java·后端·spring
研究点啥好呢33 分钟前
滴滴Go后端开发工程师面试题精选:10道高频考题+答案解析
java·开发语言·golang
ictI CABL43 分钟前
SpringBoot3.3.0集成Knife4j4.5.0实战
java
傻瓜搬砖人1 小时前
SpringMVC的请求
java·前端·javascript·spring
亚历克斯神1 小时前
Java 开发者 2026 成长路线图:从初级到架构师
java·spring·微服务
佛系彭哥1 小时前
用飞算JavaAI做项目:在线图书借阅平台设计与实现
java·飞算javaai炫技赛
A懿轩A1 小时前
Ghostty:告别 Mac 毛坯终端,打造 2026 最丝滑的 Ghostty AI 开发驾驶舱——Claude Code 团队也在用
python·macos·策略模式