类型枚举

  1. import lombok.Getter;
  2. @Getter
  3. @AllArgsConstructor
  4. public enum EventTypeEnum {
  5. WE_CHAT(0),
  6. ALI_PAY(1);
  7. private int value;
  8. }

支付接口

  1. /**
  2. * @author yuhui_cai
  3. */
  4. public interface IPayService {
  5. /**
  6. * 逻辑
  7. * @return
  8. */
  9. boolean dealEvent();
  10. /**
  11. * 获取事件类型
  12. * @return
  13. */
  14. int getType();
  15. }

阿里支付实现类

  1. import com.test.demo.textdemo.strategy.enumeration.EventTypeEnum;
  2. import com.test.demo.textdemo.strategy.service.IPayService;
  3. import org.springframework.stereotype.Service;
  4. /**
  5. * @author yuhui_cai
  6. */
  7. @Service
  8. public class AliPayServiceImpl implements IPayService {
  9. @Override
  10. public boolean dealEvent() {
  11. System.out.println("aliPay");
  12. return true;
  13. }
  14. @Override
  15. public int getType() {
  16. return EventTypeEnum.ALI_PAY.getValue();
  17. }
  18. }

微信支付实现类

  1. import com.test.demo.textdemo.strategy.enumeration.EventTypeEnum;
  2. import com.test.demo.textdemo.strategy.service.IPayService;
  3. import org.springframework.stereotype.Service;
  4. /**
  5. * @author yuhui_cai
  6. */
  7. @Service
  8. public class WeChatServiceImpl implements IPayService {
  9. @Override
  10. public boolean dealEvent() {
  11. System.out.println("weixin");
  12. return true;
  13. }
  14. @Override
  15. public int getType() {
  16. return EventTypeEnum.WE_CHAT.getValue();
  17. }
  18. }

策略类

  1. import com.test.demo.textdemo.strategy.service.IPayService;
  2. import org.springframework.stereotype.Service;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. @Service
  7. public class StrategyService {
  8. Map<Integer, IPayService> eventServiceMap = new HashMap<>();
  9. public StrategyService(List<IPayService> iStrategyServices) {
  10. for (IPayService iStrategyService : iStrategyServices) {
  11. eventServiceMap.put(iStrategyService.getType(), iStrategyService);
  12. }
  13. }
  14. public boolean dealEvent(int eventType) {
  15. IPayService iStrategyService = eventServiceMap.get(eventType);
  16. return iStrategyService.dealEvent();
  17. }
  18. }

单元测试

  1. @SpringBootTest(classes = TextDemoApplication.class)
  2. @RunWith(SpringRunner.class)
  3. public class TestDemo2 {
  4. @Autowired
  5. private StrategyService strategyService;
  6. @Test
  7. public void test(){
  8. strategyService.dealEvent(0);
  9. strategyService.dealEvent(1);
  10. }
  11. }

结果

  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".
  2. 2021-05-08 09:06:23.345 INFO 2408 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  3. 2021-05-08 09:06:23.779 INFO 2408 --- [ main] yuhui.TestDemo2 : Started TestDemo2 in 3.612 seconds (JVM running for 4.746)
  4. weixin
  5. aliPay
  6. 2021-05-08 09:06:24.014 INFO 2408 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
  7. 2021-05-08 09:06:24.015 INFO 2408 --- [extShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closing ...
  8. 2021-05-08 09:06:24.017 INFO 2408 --- [extShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed
  9. Process finished with exit code 0