设计模式之——策略模式

什么是策略模式

策略模式是对算法的包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理,最终可以实现解决多重if判断问题。

  1. 环境(Context)角色:持有一个Strategy的引用。
  2. 抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。
  3. 具体策略(ConcreteStrategy)角色:包装了相关的算法或行为。

优点:策略模式最终帮助我们解决在实际开发中多重if判断问题、提高扩展性、维护性增强、提高代码可读性。
缺点:后期维护不同策略类是非常多、定义类比较多、代码量增大。
优点大于缺点。

策略模式应用场景

比如搭建聚合支付平台的时候,这时候需要对接很多第三方支付接口,比如支付宝、微信支付、小米支付等。

通过传统if代码判断的,后期的维护性非常差!

public  String toPayHtml2(String payCode){
    if(payCode.equals("ali_pay")){
        return  "调用支付宝接口...";
    }
    if(payCode.equals("xiaomi_pay")){
        return  "调用小米支付接口";
    }
    if(payCode.equals("yinlian_pay")){
        return  "调用银联支付接口...";
    }
    return  "未找到该接口...";
}

这时候可以通过策略模式解决多重if判断问题。

策略模式架构图

调用第三方支付接口逻辑不同,但是都是最终封装为html表单参数支付接口。
在这里插入图片描述

Maven依赖信息

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.11.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <boot-version>2.1.11.RELEASE</boot-version>
    <mybatis.starter.version>2.0.0</mybatis.starter.version>
    <mapper.starter.version>2.1.5</mapper.starter.version>
</properties>

<dependencies>

    <!-- springboot 整合web组件 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 集成 lombok插件 -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- 集成 swagger 在线生成文档 -->
    <dependency>
        <groupId>com.spring4all</groupId>
        <artifactId>swagger-spring-boot-starter</artifactId>
        <version>1.7.0.RELEASE</version>
    </dependency>

    <!-- springboot-log4j -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.3.8.RELEASE</version>
    </dependency>

    <!-- 引入数据库依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- 引入mybatis-plus依赖 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.1</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <!-- springCloud 版本管理 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- springBoot 版本管理 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${boot-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- ailibaba 版本管理 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.1.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

数据库访问层

相关SQL语句

DROP TABLE IF EXISTS `payment_channel`;
CREATE TABLE `payment_channel` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `channel_name` varchar(32) NOT NULL COMMENT '渠道名称',
  `channel_id` varchar(32) NOT NULL COMMENT '渠道ID',
  `strategy_bean_id` varchar(255) DEFAULT NULL COMMENT '策略执行beanid',
  PRIMARY KEY (`id`,`channel_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='支付渠道 ';
 
-- ----------------------------
-- Records of payment_channel
-- ----------------------------
INSERT INTO `payment_channel` VALUES ('1', '支付宝渠道', 'ali_pay', 'aliPayStrategy');
INSERT INTO `payment_channel` VALUES ('2', '小米支付渠道', 'xiaomi_pay', 'xiaoMiPayStrategy');

数据库访问层

@Data
public class PaymentChannelEntity {

    /**
     * ID
     **/
    private Integer id;

    /**
     * 渠道名称
     **/
    private String channelName;

    /**
     * 渠道ID
     **/
    private String channelId;

    /**
     * 策略执行beanId
     **/
    private String strategyBeanId;

    /**
     * 是否启用
     **/
    private Integer enable;
}
@Repository
public interface PaymentChannelMapper {

    @Select("SELECT  id as id ,channel_name ,channel_id,strategy_bean_id from payment_channel where channel_id=#{payCode}")
    PaymentChannelEntity getPaymentChannel(@Param("payCode") String payCode);
}

工具类

@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

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

    /**
     * @Title: getApplicationContext
     * @Description: 获取applicationContext
     * @Date: 2021/3/25 9:44
     * @Return org.springframework.context.ApplicationContext
     **/
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * @Title: getBean
     * @Description: 通过name获取 Bean
     * @param: name bean的名称
     * @Date: 2021/3/25 9:50
     * @Return java.lang.Object
     **/
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * @Title: getBean
     * @Description: 通过class获取Bean
     * @param: clazz bean的class
     * @Date: 2021/3/25 9:55
     * @Return T
     **/
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * @Title: getBean
     * @Description: 通过name,以及Clazz返回指定的Bean
     * @param: name bean的名称
     * @param: clazz bean的class
     * @Date: 2021/3/25 9:59
     * @Return T
     **/
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

PayStrategy(抽象角色)

public interface PayStrategy {

    /**
     * @Title: toPayHtml
     * @Description: 定义共同算法实现骨架
     * @Date: 2021/3/24 19:39
     * @Return java.lang.String
     **/
    String toPayHtml();
}

ConcreteStrategy (具体实现角色)

@Component
public class AliPayStrategy implements PayStrategy {
    @Override
    public String toPayHtml() {
        return "调用支付宝接口";
    }
}
@Component
public class XiaoMiPayStrategy implements PayStrategy {
    @Override
    public String toPayHtml() {
        return "调用小米支付接口";
    }
}
@Component
public class YinlianPayStrategy implements PayStrategy {
    @Override
    public String toPayHtml() {
        return "调用银联支付接口";
    }
}

PayContextService (上下文)

@RequestMapping("/strategy")
public interface PayService {

    @GetMapping("/toPayHtml")
    String toPayHtml(String payCode);
}
@RestController
public class PayServiceImpl implements PayService {

    @Autowired
    private PaymentChannelMapper paymentChannelMapper;
    @Autowired
    private SpringUtils springUtils;

    @Override
    public String toPayHtml(String payCode) {

        //1.验证参数
        if(StringUtils.isEmpty(payCode)){
            return "payCode不能为空!";
        }

        //2.使用payCode查询数据库
        PaymentChannelEntity paymentChannel = paymentChannelMapper.getPaymentChannel(payCode);
        if(paymentChannel == null){
            return "该渠道为空!";
        }

        //3.获取策略执行的beanId
        String strategyBeanId = paymentChannel.getStrategyBeanId();

        //4.使用strategyBeanId获取对应spring容器bean信息
        PayStrategy payStrategy  = springUtils.getBean(strategyBeanId, PayStrategy.class);

        //5.执行具体策略算法
        return payStrategy.toPayHtml();
    }
}

启动类

@SpringBootApplication
@MapperScan("com.jql.*.mapper")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

在这里插入图片描述
在这里插入图片描述
更多学习资源移步到:http://www.mayikt.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在代码开发过程中,我们经常会遇到需要根据不同的条件执行不同的操作的情况。通常的做法是使用if-else语句来实现条件判断。但是,随着代码量的增加,if-else语句也会不断增多,代码变得越来越难以维护和扩展。为了解决这个问题,我们可以使用设计模式来重构代码,使其更加清晰、简洁和易于扩展。 状态模式是一种行为型设计模式,它允许对象在内部状态改变时改变它的行为。状态模式将状态封装成独立的类,并将请求委托给当前的状态对象。当状态发生改变时,更改状态对象以及委托给它的请求处理。 下面我们以一个简单的电梯控制器为例,来演示如何使用状态模式来代替if-else语句。 首先,我们定义一个抽象的状态类: ```python class LiftState: def open(self): pass def close(self): pass def run(self): pass def stop(self): pass ``` 然后,我们定义具体的状态类,分别代表电梯处于打开、关闭、运行和停止状态: ```python class OpenState(LiftState): def open(self): print("电梯门已经打开了,不需要再打开了!") def close(self): print("电梯门关闭...") return CloseState() def run(self): print("电梯还没关门,不能上下楼!") def stop(self): print("电梯门已经打开了,不能停止!") class CloseState(LiftState): def open(self): print("电梯门打开...") return OpenState() def close(self): print("电梯门已经关闭了,不需要再关闭了!") def run(self): print("电梯开始运行...") return RunState() def stop(self): print("电梯已经停止了,不能再停止了!") class RunState(LiftState): def open(self): print("电梯正在运行,不能打开门!") def close(self): print("电梯正在运行,不能关闭门!") def run(self): print("电梯正在运行...") def stop(self): print("电梯停止运行...") return StopState() class StopState(LiftState): def open(self): print("电梯门打开...") return OpenState() def close(self): print("电梯已经停止了,不能关闭门!") def run(self): print("电梯开始运行...") return RunState() def stop(self): print("电梯已经停止了,不需要再停止了!") ``` 最后,我们定义一个电梯控制器类,用于控制电梯的状态: ```python class LiftController: def __init__(self): self.state = StopState() def set_state(self, state): self.state = state def open(self): self.state = self.state.open() def close(self): self.state = self.state.close() def run(self): self.state = self.state.run() def stop(self): self.state = self.state.stop() ``` 现在,我们可以通过调用电梯控制器类的方法来控制电梯的状态,而无需使用if-else语句: ```python lift_controller = LiftController() lift_controller.open() # 电梯门打开... lift_controller.close() # 电梯门关闭... lift_controller.run() # 电梯开始运行... lift_controller.stop() # 电梯停止运行... ``` 通过使用状态模式,我们可以将电梯控制器的不同状态封装成独立的类,使代码更加清晰、简洁和易于扩展。此外,状态模式还可以减少if-else语句的使用,提高代码的可读性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值