java设计模式之:策略模式+工厂模式整合案例实战(一)
创始人
2025-01-15 03:06:48
0次

本文介绍项目中常用的策略模式+工厂模式的案例,该案例是针对策略类比较少的情况;下一篇会讲解策略类比较多的案例,下面直接开始:

案例1:项目中对系统中的客户和销售进行事件通知(短信、邮件、钉钉)

首先要有通知的策略接口,接口里面要有一个方法就是通知的方法

public interface PushChannelStrategy{    // 通知方法    SendResult send(MessagePushParam param,BaseMsg baseMsg);  }

有了接口那必然有实现类,三个实现类短信、邮件、钉钉

短信:

@Slf4j @Component public class SmsPushChannelStrategy implements PushChannelStrategy{    @Autowired    private NoticeClient noticeClient;        @Override    public SendResult send(MessagePushParam param,BaseMsg baseMsg){        //1、before send check        //2、send sms        NoticeResult noticeResult=noticeClient.sendSms(xxxx);        sendResult.setMessageStatus(MessageStatusEnum.SUCCESS);        return sendResult;    } }

邮件:

@Slf4j @Component public class EmailPushChannelStrategy implements PushChannelStrategy{    @Autowired    private NoticeClient noticeClient;        @Override    public SendResult send(MessagePushParam param,BaseMsg baseMsg){        //1、before send check        //2、send email        Email emailMsg=(Email)baseMsg;        NoticeResult noticeResult=noticeClient.sendEmail(xxxx);        sendResult.setMessageStatus(MessageStatusEnum.SUCCESS);        return sendResult;    } }

钉钉:

@Slf4j @Component public class DingTalkPushChannelStrategy implements PushChannelStrategy{    @Autowired    private DingTalkClient dingTalkClient;        @Override    public SendResult send(MessagePushParam param,BaseMsg baseMsg){        //1、before send check        //2、send ding talk        SendResult sendResult=dingTalkClient.send(xxx);        // 其他结果参数组装        return sendResult;    } }

然后通过策略工厂来获取具体的策略类:(由于只有三个策略类,所以通过注入的方式对channel进行判断)

@Component public class PushChannelStrategyFactory{     @Autowired      private DingTalkPushChannelStrategy dingTalkPushChannelStrategy;     @Autowired     private SmsPushChannelStrategy smsPushChannelStrategy;     @Autowired     private EmailPushChannelStrategy emailPushChannelStrategy;          public PushChannelStrategy getStrategy(PushChannel pushChannel){         switch(pushChannel){            case DING_TALK:               return dingTalkPushChannelStrategy;            case SMS:               return smsPushChannelStrategy;            case EMAIL:               return emailPushChannelStrategy;            default:               throw new RuntimeException("不支持的类型");            }      } }

当然策略工厂针对策略实现类比较少的情况还可以这样写:

@Component public class PushChannelStrategyFactory2{     @Autowired     private DingTalkPushChannelStrategy dingTalkPushChannelStrategy;     @Autowired     private SmsPushChannelStrategy smsPushChannelStrategy;     @Autowired     private EmailPushChannelStrategy emailPushChannelStrategy;          private static final Map pushChannelBuilderMap=new HashMap<>();          @PostConstruct     public void init(){         pushChannelBuilderMap.put(PushChannel.SMS,smsPushChannelStrategy);         pushChannelBuilderMap.put(PushChannel.Email,emailPushChannelStrategy);         pushChannelBuilderMap.put(PushChannel.DING_TALK,dingTalkPushChannelStrategy);       }     Public PushChannelStrategy getStrategy(PushChannel PushChannel){       if(PushChannel==null){           return null;        }        return pushChannelBuilderMap.get(PushChannel);     } }        

用到的枚举类:

@Getter public enum PushChannel{    SMS("sms","短信"),    EMAIL("email","邮件"),    DING_TALK("dingTalk","钉钉");        private final String value;        PushChannel(String value,String desc){       this.value=value;     }     public static PushChannel getPushChannel(String pushChannel){       if(pushChannel==null){          return null;       }       for(PushChannel channel:PushChannel.values()){         if(pushChannel.equals(channel.getValue())){            return channel;        }    }     return null;   } }    

在使用的时候通过策略工厂里面的方法获取具体的策略类:

@Slf4j @Service  public class MessagePushService{      @Autowired      private PushChannelStrategyFactory pushChannelStrategyFactory;      @Autowired      private MessageRecordRepository messageRecordRepository;            public ResultDTO pushSync(MessagePushCommand command){         MessagePushParam messagePushParam =MessagePushAssembler.convert(command);         //1,业务逻辑处理         //2、根据渠道进行触达         PushChannel pushChannel=messagePushParam.getChannel();         if(pushChannel==null){           throw new MessagePushException(xxx);         }         //3、获取具体的策略类         PushChannelStrategy pushChannelStrategy=pushChannelStrategyFactory.getStrategy(pushChannel);         SendResult sendResult=PushChannelStrategy.send(messagePushParam,xxx);         //4,记录落库         return ResultDTO.getSuccessResult(true);     }  }

到此该版本的策略模式+工厂模式就结束了,欢迎点评和指出不足之处。

相关内容

热门资讯

实测交流透视挂!宝玛大厅ai智... 实测交流透视挂!宝玛大厅ai智能透视挂教程,宝玛大厅破解软件经验(本来有挂辅助)1、每一步都需要思考...
总算了解辅助器!朱雀吴江三缺一... 总算了解辅助器!朱雀吴江三缺一ai智能透视辅助挂技巧,朱雀吴江三缺一好像是有挂(有辅助挂细节)朱雀吴...
今日公布透视!九五至尊大厅ai... 今日公布透视!九五至尊大厅ai智能透视插件,九五至尊大厅有没有透视挂窍门(切实有挂技术)1、在九五至...
玩家必看科普开挂!聚闲麻将ai... 玩家必看科普开挂!聚闲麻将ai智能透视辅助挂软件,聚闲麻将总是真的有挂(有辅助挂方式)聚闲麻将能透视...
据报道开挂!旅游娱乐ai智能开... 据报道开挂!旅游娱乐ai智能开挂工具,旅游娱乐辅助工具演示(一直有挂教学)1、全新机制【旅游娱乐ai...
一分钟揭秘开挂!决胜朋来麻将a... 一分钟揭秘开挂!决胜朋来麻将ai智能透视辅助挂教程,决胜朋来麻将真是真的是有挂(有辅助挂总结)1、首...
如何分辨真伪透视!红牛ai智能... 如何分辨真伪透视!红牛ai智能透视攻略,红牛是不是有挂经验(真是有挂技术)1、操作简单,无需红牛手机...
2分钟细说辅助器!江湖大庆麻将... 2分钟细说辅助器!江湖大庆麻将ai智能透视辅助挂攻略,江湖大庆麻将竟然是真的有挂(有辅助挂头条)1、...
总算清楚透视挂!老夫子厅ai智... 总算清楚透视挂!老夫子厅ai智能透视挂技巧,老夫子厅怎么开挂课程(好像发现有挂)老夫子厅破解侠是真的...
解密关于辅助!菜鸟麻将ai智能... 解密关于辅助!菜鸟麻将ai智能透视辅助挂脚本,菜鸟麻将总是存在有挂(有辅助挂存在)1、用户打开应用后...