策略设计模式

策略设计模式(Strategy Pattern)是一种行为型设计模式,它定义了一系列算法,并将每个算法封装起来,使得它们可以互相替换,且算法的变化不会影响使用算法的客户。这个模式使得算法可以在不影响客户端的情况下发生变化。

关键角色

  1. 抽象策略(Strategy):定义所有支持的算法的公共接口。
  2. 具体策略(Concrete Strategy):实现具体的算法。
  3. 上下文(Context):维护一个策略类的引用,用于调用具体的算法。

示例

以下以登录为示例,演示策略设计模式。

java 复制代码
package com.example.study.pattern;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;;

@Slf4j
@SpringBootTest
class ApplicationTests {

    @Test
    void contextLoads() {

        LoginStrategy usernamePasswordLoginStrategy = new UsernamePasswordLogin("lili", "123456");
        LoginContext lili = new LoginContext(usernamePasswordLoginStrategy);
        lili.login();

        LoginStrategy verificationCodeLoginStrategy = new VerificationCodeLogin("18655141030", "225433");
        LoginContext tom = new LoginContext(verificationCodeLoginStrategy);
        tom.login();
    }

    interface LoginStrategy {
        void login();
    }

    class UsernamePasswordLogin implements LoginStrategy {

        private String username;
        private String password;

        public UsernamePasswordLogin(String username, String password) {
            this.username = username;
            this.password = password;
        }

        @Override
        public void login() {
            log.info("用户名密码登录, 用户:{}", this.username);
        }
    }

    class VerificationCodeLogin implements LoginStrategy {

        private String phone;
        private String verificationCode;

        public VerificationCodeLogin(String phone, String verificationCode) {
            this.phone = phone;
            this.verificationCode = verificationCode;
        }

        @Override
        public void login() {
            log.info("手机验证码登录, 用户:{}", this.phone);
        }
    }

    class LoginContext {
        private LoginStrategy loginStrategy;

        public LoginContext(LoginStrategy loginStrategy) {
            this.loginStrategy = loginStrategy;
        }

        public void login() {
            loginStrategy.login();
        }
    }

}

运行结果

策略模式的优点

  1. 易于切换算法:可以在运行时切换不同的算法。
  2. 避免使用条件判断:通过使用策略类代替条件语句(如 if-else 或 switch-case)。
  3. 开放封闭原则:新增算法时只需增加新的策略类,不需要修改现有代码。

适用场景

  1. 多个类只区别在表现行为不同:可以使用策略模式来动态选择具体的行为。
  2. 需要在不同情况下使用不同的算法:如不同的排序算法、不同的加密算法等。
  3. 避免使用条件判断语句:可以将条件判断语句替换为策略模式中的策略类。

策略设计模式通过将算法的实现与使用分离,提高了代码的灵活性和可维护性,在各种需要灵活切换算法的场景中得到了广泛应用。

相关推荐
lcj251114 小时前
【日志系统实战】同步/异步日志系统:手写日志等级模块,吃透enum class与7大分级,面试不慌
c++·设计模式·面试·c++11·日志系统
电子科技圈18 小时前
UCIe桥接IP的选型与验证:行业高频问题解析
设计模式·软件工程·软件构建·设计规范
Bode_200219 小时前
交互系统研究图谱
设计模式·设计方法
李航198320 小时前
Codex现代风格卧室设计案例1
人工智能·python·计算机视觉·设计模式·ai
善良勤劳勇敢而又聪明的老杨1 天前
【AI编程系列】RAG 常用设计模式
设计模式·ai编程
Meteors.1 天前
Android 项目常见问题与 23 种设计模式应用对照表(含 Kotlin 示例)
android·设计模式·kotlin
Mh2 天前
基于踩过的坑,浅聊一下vh、svh、lvh、dvh
前端·css·设计模式
瑶池酒剑仙4 天前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
HarmonSir6 天前
Codex 架构解析:一条对话如何长成一个运行时系统
后端·设计模式·架构
饮水桶_T7 天前
Java调用大语言模型 笔记
笔记·设计模式·电脑·ai编程