zoukankan      html  css  js  c++  java
  • 访问者模式运用

    系统有如下需求:

    公告:需要保存以及用极光推送

    活动:和通知一样

    评论:暂时和公告一样,可能需求会变化

    关注:暂时和公告一样,可能需求会变化

    服务:短信、微信、存储,不用极光推送

    用一个矩阵图表示如下:

     短信微信极光推送存储
    公告    
    活动    
    评论    
    关注    
    服务(各种类型消息)

    需求会在两个维度上进行扩展

    1. 新的通知类型,比如订单什么的

    2.新的通知手段,比如邮件什么的

    同时原先的通知手段也有可能发生改变,比如关注支持短信推送什么的,公告支持微信推送

    这时候用访问者模式可以很少地满足需求

    使用访问者模式前代码

    public class RemoteMessageListener {
     
     @Autowired
     private JPushClient pushClient;
     
     @Autowired
     private WxTemplateService templateService;
     
     @Autowired
     private UserWeiXinBindService bindService;
     
     @Autowired
     private ProfileService profileService;
     
     @Value("${wechat.config.templateId:-}")
     private String templateId;
     
     @Autowired
     private SmsService smsService;
     
     @Autowired
     private NoticeService noticeService;
     
     @Autowired
     private WechatUrl wechatUrl;
     
     // 处理公告消息
     @RabbitHandler
     public void handleMessage(AnnounceMessage msg){
     log.info(msg.getTitle());
     if(msg.getGuideVisible()) {
     noticeService.save(msg, NoticeGroup.Announce);
     try {
     pushClient.sendNotificationAll(msg.getBody());
     } catch (APIConnectionException e) {
     e.printStackTrace();
     } catch (APIRequestException e) {
     e.printStackTrace();
     }
     }
     }
     
     // 处理活动消息
     @RabbitHandler
     public void handleMessage(ActivityMessage msg){
     log.info(msg.getTitle());
     if(msg.getGuideVisible()) {
     noticeService.save(msg, NoticeGroup.Activity);
     try {
     pushClient.sendNotificationAll(msg.getBody());
     } catch (APIConnectionException e) {
     e.printStackTrace();
     } catch (APIRequestException e) {
     e.printStackTrace();
     }
     }
     }
     
     // 处理外部导游录用消息
     @RabbitHandler
     public void handleMessage(GuideHiredMessage notice) {
     
     log.info("------【监听器】 开始处理录用导游消息------");
     notice.setNoticeType("guide_hired");
     notice.setCreateTime(LocalDateTime.now());
     SimpleProfileDto simpleProfileDto = profileService.getSimpleById(notice.getUserId());
     if(simpleProfileDto != null) {
     noticeService.save(notice, NoticeGroup.Service, simpleProfileDto.getUserId());
     UserWeiXinBindDto bindDto = bindService.findByUserId(notice.getUserId());
     if (bindDto != null) {
     WxTemplate wxTemplate = WxTemplate
     .newTemplate(bindDto.getOpenId(), templateId, wechatUrl.getDelegationUrl(notice.getDelegationId()), "#8F8F8F")
     .addItem(new WxTemplateItem("first", "您好," + notice.getAgent() + "给您派团啦。", "#8F8F8F"))
     .addItem(new WxTemplateItem("keyword1", notice.getTitle(), "#8F8F8F"))
     .addItem(new WxTemplateItem("keyword2", this.getFormatDate(notice.getStartTime(), notice.getEndTime()), "#8F8F8F"))
     .addItem(new WxTemplateItem("keyword3", this.getGuideType(notice.getGuideType()), "#8F8F8F"))
     .addItem(new WxTemplateItem("remark", "请做好带团准备工作!", "#8F8F8F"));
     templateService.sendMessage(wxTemplate);
     }
     
     // 发短信
     String content = "【" + notice.getAgent() + "】已经录用您!请提前做好带团准备工作!";
     smsService.send(simpleProfileDto.getMobile(), content);
     }
     }
     
     // 处理外部导游撤销录用消息
     @RabbitHandler
     public void handleMessage(GuideHiredRevokeMessage notice) {
     
     log.info("------【监听器】 开始处理撤销录用导游消息------");
    notice.setNoticeType("guide_hired_revoke");
    notice.setCreateTime(LocalDateTime.now());
    SimpleProfileDto simpleProfileDto = profileService.getSimpleById(notice.getUserId());
    if(simpleProfileDto != null) {
    noticeService.save(notice, NoticeGroup.Service, simpleProfileDto.getUserId());
     
    //发短信
    String content = "【" + notice.getAgent() + "】已经撤销对您的录用!";
    smsService.send(simpleProfileDto.getMobile(), content);
    }
    }
     
    // 处理内部导游指派消息
    @RabbitHandler
    public void handleMessage(GuideAssignedMessage notice) {
     
    log.info("行程ID:{}", notice.getScheduleId());
    log.info("------【监听器】 开始处理内部导游指派消息------");
    notice.setNoticeType("guide_assigned");
    notice.setCreateTime(LocalDateTime.now());
     
    SimpleProfileDto simpleProfileDto = profileService.findSimpleByMobile(notice.getMobile());
    if(simpleProfileDto != null) {
    notice.setUserId(simpleProfileDto.getUserId()); //找到用户ID
    noticeService.save(notice, NoticeGroup.Service, simpleProfileDto.getUserId());
    UserWeiXinBindDto bindDto = bindService.findByUserId(simpleProfileDto.getUserId());
    if (bindDto != null) {
    WxTemplate wxTemplate = WxTemplate
    .newTemplate(bindDto.getOpenId(), templateId, wechatUrl.getTravelUrl(notice.getScheduleId()), "#8F8F8F")
    .addItem(new WxTemplateItem("first", "您好," + notice.getAgent() + "给您派团啦。", "#8F8F8F"))
    .addItem(new WxTemplateItem("keyword1", notice.getTitle(), "#8F8F8F"))
    .addItem(new WxTemplateItem("keyword2", this.getFormatDate(notice.getStartTime(), notice.getEndTime()), "#8F8F8F"))
    .addItem(new WxTemplateItem("keyword3", this.getGuideType(notice.getGuideType()), "#8F8F8F"))
    .addItem(new WxTemplateItem("remark", "请做好带团准备工作!", "#8F8F8F"));
    templateService.sendMessage(wxTemplate);
    }
    }
     
    //不管是不是系统用户,都要发短信
    String content = "【" + notice.getAgent() + "】已经录用您!请提前做好带团准备工作!";
    smsService.send(notice.getMobile(), content);
    }
     
    // 处理内部导游撤销指派事件
    @RabbitHandler
    public void handleMessage(GuideAssigneRevokeMessage notice) {
     
    log.info("------【监听器】 开始处理撤销内部导游指派消息------");
    notice.setNoticeType("guide_assigned_revoke");
    notice.setCreateTime(LocalDateTime.now());
    SimpleProfileDto simpleProfileDto = profileService.findSimpleByMobile(notice.getMobile());
    if(simpleProfileDto != null) {
    notice.setUserId(simpleProfileDto.getUserId()); //找到用户ID
    noticeService.save(notice, NoticeGroup.Service, simpleProfileDto.getUserId());
    }
     
    //不管是不是系统用户,都要发短信
    String content = "【" + notice.getAgent() + "】已经撤销对您的录用!";
    smsService.send(notice.getMobile(), content);
    }
     
    @RabbitHandler
    public void handleMessage(AuthenticateMessage notice) {
     
    log.info("------【监听器】 开始处理管理后台导游认证消息------");
    notice.setNoticeType("guide_authenticate");
    SimpleProfileDto simpleProfileDto = profileService.findSimpleByMobile(notice.getMobile());
    if(simpleProfileDto != null) {
    notice.setCreateTime(LocalDateTime.now());
    noticeService.save(notice, NoticeGroup.Service, simpleProfileDto.getUserId());
    smsService.send(simpleProfileDto.getMobile(), notice.getContent());
    }
    }
     
    private String getFormatDate(LocalDateTime start, LocalDateTime end){
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月d日");
    String date = String.format("%s - %s",
    start.format(formatter),
    end.format(formatter));
    return date;
    }
     
    private String getGuideType(Integer guideType) {
    switch (guideType) {
    case 0:
    return "不限";
    case 1:
    return "领队";
    case 2:
    return "全陪";
    case 3:
    return "地陪";
    }
    return "未知";
    }
    }

    使用访问者模式后代码:

    public class RemoteMessageListener {

    @Autowired
     private NoticeStoreService storeService;

    @Autowired
     private WechatPushService wechatPushService;

    @Autowired
     private SmsService smsService;

    @Autowired
     private JPushService jPushService;

    // 处理公告消息
     @RabbitHandler
     public void handleMessage(AnnounceMessage notice){
    log.info(notice.getTitle());
    if(notice.getGuideVisible()) {
    notice.accept(storeService);
    notice.accept(jPushService);
    }
    }

    // 处理活动消息
     @RabbitHandler
     public void handleMessage(ActivityMessage notice){
    log.info(notice.getTitle());
    if(notice.getGuideVisible()) {
    notice.accept(storeService);
    notice.accept(jPushService);
    }
    }

    // 处理外部导游录用消息
     @RabbitHandler
     public void handleMessage(GuideHiredMessage notice) {

    log.info("------【监听器】 开始处理录用导游消息------");
    notice.setNoticeType("guide_hired");
    notice.setCreateTime(LocalDateTime.now());
    notice.accept(storeService);
    notice.accept(wechatPushService); // 微信推送
     notice.accept(smsService); //发短信
     }

    // 处理外部导游撤销录用消息
     @RabbitHandler
     public void handleMessage(GuideHiredRevokeMessage notice) {

    log.info("------【监听器】 开始处理撤销录用导游消息------");
    notice.setNoticeType("guide_hired_revoke");
    notice.setCreateTime(LocalDateTime.now());
    notice.accept(storeService);
    notice.accept(wechatPushService);
    notice.accept(smsService);
    }

    // 处理内部导游指派消息
     @RabbitHandler
     public void handleMessage(GuideAssignedMessage notice) {

    log.info("行程ID:{}", notice.getScheduleId());
    log.info("------【监听器】 开始处理内部导游指派消息------");
    notice.setNoticeType("guide_assigned");
    notice.setCreateTime(LocalDateTime.now());

    notice.accept(storeService);
    notice.accept(wechatPushService);
    notice.accept(smsService);
    }

    // 处理内部导游撤销指派事件
     @RabbitHandler
     public void handleMessage(GuideAssigneRevokeMessage notice) {

    log.info("------【监听器】 开始处理撤销内部导游指派消息------");
    notice.setNoticeType("guide_assigned_revoke");
    notice.setCreateTime(LocalDateTime.now());
    notice.accept(storeService);
    notice.accept(wechatPushService);
    notice.accept(smsService);
    }

    // 处理审核认证事件
     @RabbitHandler
     public void handleMessage(AuthenticateMessage notice) {

    log.info("------【监听器】 开始处理管理后台导游认证消息------");
    notice.setNoticeType("guide_authenticate");
    notice.setCreateTime(LocalDateTime.now());
    notice.accept(storeService);
    notice.accept(wechatPushService);
    notice.accept(smsService);
    }

    可以看出,使用访问者模式之后,代码更加逻辑更加清晰,职责更加明确,同时又能满足扩展性

  • 相关阅读:
    嵌入式:指针的指针、链表、UCOS 的 OSMemCreate 。
    嵌入式&iOS:回调函数(C)与block(OC)传 参/函数 对比
    嵌入式&iOS:回调函数(C)与block(OC)回调对比
    iOS:以前笔记,未整理版。太多了,先放着吧。。。。。。。
    嵌入式:小技巧(慢慢回忆更新)(16.12.17更)
    嵌入式:J-link刷固件(坑)
    iOS:GCD组
    Android Studio教程07-Fragment的使用
    Android Studio教程06-布局,监听器以及基本控件
    Android Studio教程05-Parcelables和Bundles.md
  • 原文地址:https://www.cnblogs.com/mkxzy/p/7091375.html
Copyright © 2011-2022 走看看