系统有如下需求:
公告:需要保存以及用极光推送
活动:和通知一样
评论:暂时和公告一样,可能需求会变化
关注:暂时和公告一样,可能需求会变化
服务:短信、微信、存储,不用极光推送
用一个矩阵图表示如下:
短信 | 微信 | 极光推送 | 存储 | |
---|---|---|---|---|
公告 | 是 | 是 | ||
活动 | 是 | 是 | ||
评论 | 是 | 是 | ||
关注 | 是 | 是 | ||
服务(各种类型消息) | 是 | 是 | 否 | 是 |
需求会在两个维度上进行扩展
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();
}
}
}