策略模式

类型枚举

import lombok.Getter;

@Getter

public enum EventTypeEnum {

    WE_CHAT(0),

    ALI_PAY(1);

    private int value;

    EventTypeEnum(int value) {

        this.value = value;

    }

}

支付接口

/**

* @author yuhui_cai

*/

public interface IPayService {

    /**

    * 程序逻辑

    * @return

    */

    boolean dealEvent();

    /**

    * 获取事件类型

    * @return

    */

    int getType();

}

阿里支付实现类

import com.test.demo.textdemo.strategy.enumeration.EventTypeEnum;

import com.test.demo.textdemo.strategy.service.IPayService;

import org.springframework.stereotype.Service;

/**

* @author yuhui_cai

*/

@Service

public class AliPayServiceImpl implements IPayService {

    @Override

    public boolean dealEvent() {

        System.out.println("aliPay");

        return true;

    }

    @Override

    public int getType() {

        return EventTypeEnum.ALI_PAY.getValue();

    }

}

微信支付实现类

import com.test.demo.textdemo.strategy.enumeration.EventTypeEnum;

import com.test.demo.textdemo.strategy.service.IPayService;

import org.springframework.stereotype.Service;

/**

* @author yuhui_cai

*/

@Service

public class WeChatServiceImpl implements IPayService {

    @Override

    public boolean dealEvent() {

        System.out.println("weixin");

        return true;

    }

    @Override

    public int getType() {

        return EventTypeEnum.WE_CHAT.getValue();

    }

}

策略类

import com.test.demo.textdemo.strategy.service.IPayService;

import org.springframework.stereotype.Service;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

@Service

public class StrategyService {

    Map<Integer, IPayService> eventServiceMap = new HashMap<>();

    public StrategyService(List<IPayService> iStrategyServices) {

        for (IPayService iStrategyService : iStrategyServices) {

            eventServiceMap.put(iStrategyService.getType(), iStrategyService);

        }

    }

    public boolean dealEvent(int eventType) {

        IPayService iStrategyService = eventServiceMap.get(eventType);

        return iStrategyService.dealEvent();

    }

}

单元测试

@SpringBootTest(classes = TextDemoApplication.class)

@RunWith(SpringRunner.class)

public class TestDemo2 {

    @Autowired

    private StrategyService strategyService;

    @Test

    public void test(){

        strategyService.dealEvent(0);

        strategyService.dealEvent(1);

    }

}

运行结果

2021-05-08 09:06:22.625  WARN 2408 --- [          main] c.b.m.core.metadata.TableInfoHelper      : Can not find table primary key in Class: "com.test.demo.textdemo.entity.PmsSystemDatabase".

2021-05-08 09:06:23.345  INFO 2408 --- [          main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'

2021-05-08 09:06:23.779  INFO 2408 --- [          main] yuhui.TestDemo2                          : Started TestDemo2 in 3.612 seconds (JVM running for 4.746)

weixin

aliPay

2021-05-08 09:06:24.014  INFO 2408 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

2021-05-08 09:06:24.015  INFO 2408 --- [extShutdownHook] com.alibaba.druid.pool.DruidDataSource  : {dataSource-1} closing ...

2021-05-08 09:06:24.017  INFO 2408 --- [extShutdownHook] com.alibaba.druid.pool.DruidDataSource  : {dataSource-1} closed

Process finished with exit code 0

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 参考资料:漫话:如何给女朋友解释什么是策略模式? 设计模式之策略模式(实例+Demo) Java设计模式(8)——...
    帮我的鸵鸟盖个章阅读 389评论 0 1
  • 前言:策略模式和适配器模式很像 但前者策略的接口和相关类会暴露出来,并且每个策略的“计算内容”都不同【常用于计...
    程序员ken阅读 1,043评论 0 0
  • 通过不同的路,到达相同的目的地,这就是策略模式。 使用不同的策略,完成最终相同的结果。 如:使用不同的优惠券,最总...
    守拙者_6a98阅读 188评论 0 0
  • if/else过长的问题 什么样才算if/else过长呢?每个人或许都有不同的定义吧,但是最为直观的感受就是当你看...
    沙漠小舟阅读 601评论 0 0
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,605评论 28 53