策略模式-实现方式三

一 枚举类

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



}

源代码

相关推荐
奶油话梅糖43 分钟前
打开项目 0.9 秒、内存只有 IDEA 的 1/12:OpenIDEA 用 Rust 把 IntelliJ IDEA 重造了一遍
java·rust·github·intellij-idea
vipxieliang1 小时前
语言对比卡:PHP vs Go vs Java 的优缺点、适用场景与「为什么选 PHP」
java·go·php
Rain的Java大神之路1 小时前
公交App上“还有2站,约5分钟”怎么实现?90%的人答错这道大厂面试题:GPS漂移、ETA不准、10万QPS热点读全拆透(附完整代码)
java·后端·面试
golang学习记1 小时前
IDEA 2026.3 远程开发迎来重磅升级
java·ide·intellij-idea·远程开发
天天被压力1 小时前
【A股分钟级高频数据完全手册 #04】指数分钟级行情数据接口:沪深指数Pro版最新分时取数
java·人工智能·python
敲代码的小小酥2 小时前
谈SpringMVC与Spring WebFlux的区别与适用场景
java·后端·spring
天天被压力2 小时前
【A股分钟级高频数据完全手册 #03】股票历史1分钟K线数据接口怎么用:Pro版批量拉取与本地缓存
java·人工智能·python
apcipot_rain2 小时前
C++转Java日记(02)
java·开发语言·c++
nice_lcj5202 小时前
【从零理解 Spring AI · 03】Spring AI 在 Spring 生态里的位置
java·spring boot·大模型·spring ai·ai应用开发
码农小河6612 小时前
基于SSM+JSP的高校专业信息管理系统【附源码+数据库】
java·开发语言·数据库