网站建设素材收集通知,涿鹿县建设局网站,网站 固定ip,信息服务平台有哪些本文介绍项目中常用的策略模式工厂模式的案例#xff0c;该案例是针对策略类比较少的情况#xff1b;下一篇会讲解策略类比较多的案例#xff0c;下面直接开始#xff1a;
案例1#xff1a;项目中对系统中的客户和销售进行事件通知(短信、邮件、钉钉)
首先要有通知的策略…本文介绍项目中常用的策略模式工厂模式的案例该案例是针对策略类比较少的情况下一篇会讲解策略类比较多的案例下面直接开始
案例1项目中对系统中的客户和销售进行事件通知(短信、邮件、钉钉)
首先要有通知的策略接口接口里面要有一个方法就是通知的方法
public interface PushChannelStrategy// 通知方法SendResult send(MessagePushParam paramBaseMsg baseMsg;
有了接口那必然有实现类三个实现类短信、邮件、钉钉
短信
Slf4j
Component
public class SmsPushChannelStrategy implements PushChannelStrategyAutowiredprivate NoticeClient noticeClientOverridepublic SendResult sendMessagePushParam paramBaseMsg baseMsg//1、before send check//2、send smsNoticeResult noticeResultnoticeClient.sendSmsxxxxsendResult.setMessageStatusMessageStatusEnum.SUCCESSreturn sendResult邮件
Slf4j
Component
public class EmailPushChannelStrategy implements PushChannelStrategyAutowiredprivate NoticeClient noticeClientOverridepublic SendResult sendMessagePushParam paramBaseMsg baseMsg//1、before send check//2、send emailEmail emailMsg(Email)baseMsgNoticeResult noticeResultnoticeClient.sendEmailxxxxsendResult.setMessageStatusMessageStatusEnum.SUCCESSreturn sendResult钉钉
Slf4j
Component
public class DingTalkPushChannelStrategy implements PushChannelStrategyAutowiredprivate DingTalkClient dingTalkClientOverridepublic SendResult sendMessagePushParam paramBaseMsg baseMsg//1、before send check//2、send ding talkSendResult sendResultdingTalkClient.send(xxx)// 其他结果参数组装return sendResult然后通过策略工厂来获取具体的策略类(由于只有三个策略类所以通过注入的方式对channel进行判断)
Component
public class PushChannelStrategyFactoryAutowired private DingTalkPushChannelStrategy dingTalkPushChannelStrategyAutowiredprivate SmsPushChannelStrategy smsPushChannelStrategyAutowiredprivate EmailPushChannelStrategy emailPushChannelStrategypublic PushChannelStrategy getStrategy(PushChannel pushChannel)switch(pushChannel)case DING_TALKreturn dingTalkPushChannelStrategycase SMSreturn smsPushChannelStrategycase EMAILreturn emailPushChannelStrategydefaultthrow new RuntimeException(不支持的类型)当然策略工厂针对策略实现类比较少的情况还可以这样写
Component
public class PushChannelStrategyFactory2Autowiredprivate DingTalkPushChannelStrategy dingTalkPushChannelStrategy;Autowiredprivate SmsPushChannelStrategy smsPushChannelStrategy;Autowiredprivate EmailPushChannelStrategy emailPushChannelStrategy;private static final MapPushChannel,PushChannelStrategy pushChannelBuilderMapnew HashMap();PostConstructpublic void init(){pushChannelBuilderMap.put(PushChannel.SMS,smsPushChannelStrategy);pushChannelBuilderMap.put(PushChannel.Email,emailPushChannelStrategy);pushChannelBuilderMap.put(PushChannel.DING_TALK,dingTalkPushChannelStrategy);}Public PushChannelStrategy getStrategy(PushChannel PushChannel){if(PushChannelnull){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.valuevalue;}public static PushChannel getPushChannel(String pushChannel){if(pushChannelnull){return null;}for(PushChannel channel:PushChannel.values()){if(pushChannel.equals(channel.getValue())){return channel;}}return null;}
}
在使用的时候通过策略工厂里面的方法获取具体的策略类
Slf4j
Servicepublic class MessagePushService{Autowiredprivate PushChannelStrategyFactory pushChannelStrategyFactory;Autowiredprivate MessageRecordRepository messageRecordRepository;public ResultDTOBoolean pushSync(MessagePushCommand command){MessagePushParam messagePushParam MessagePushAssembler.convert(command);//1,业务逻辑处理//2、根据渠道进行触达PushChannel pushChannelmessagePushParam.getChannel();if(pushChannelnull){throw new MessagePushException(xxx);}//3、获取具体的策略类PushChannelStrategy pushChannelStrategypushChannelStrategyFactory.getStrategy(pushChannel);SendResult sendResultPushChannelStrategy.send(messagePushParam,xxx);//4,记录落库return ResultDTO.getSuccessResulttrue;
到此该版本的策略模式工厂模式就结束了欢迎点评和指出不足之处。