基于业务场景,我需要使用if else 来判断是那种类型,然后选择调用微信小程序还是 app的消息推送,所以为了替换if else,也为了后期代码维护简单,现在使用个简单的工厂模式+策略模式 来替换if else
1、先构建一个接口,然后定义好要调用的方法
package cn.lingyue.charging.service;
import cn.lingyue.charging.model.requestModel.NoticeRequest;
/**
* created by king on 2020/6/18 6:03 下午
*/
public interface ISendMsgService {
boolean sendMsg(Object object);
}
2、构建实现类,并重写调用方法,现在有两个实现类AppSendMsgServiceImpl、MiniSendMsgServiceImpl,此处要注意,需要@Component注解,注意service是否通过@Autowired注解成功,这里修改了service的构建方式,构建了静态构造方法,调用的时候也选择了 类名.属性.方法名
package cn.lingyue.charging.service.impl;
import cn.lingyue.charging.model.requestModel.NoticeRequest;
import cn.lingyue.charging.service.ISendMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* app消息推送接口
* created by king on 2020/6/18 6:09 下午
*/
@Slf4j
@Component
public class AppSendMsgServiceImpl implements ISendMsgService {
// 此处为app的推送消息渠道接口,开发的时候,自动换成对应的实现接口即可~
@Autowired
private NoticeInfoServiceImpl noticeInfoService;
private static AppSendMsgServiceImpl appSendMsgService;
@PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
public void init() {
appSendMsgService = this;
appSendMsgService.noticeInfoService = this.noticeInfoService;
// 初使化时将已静态化的testService实例化
}
@Override
public boolean sendMsg(Object object) {
// 此处为app的推送消息渠道,开发的时候,自动换成对应的实现接口即可~
// return appSendMsgService.noticeInfoService.send((NoticeRequest)object);
return false;
}
}
package cn.lingyue.charging.service.impl;
import cn.lingyue.charging.model.requestModel.NoticeRequest;
import cn.lingyue.charging.service.ICommonService;
import cn.lingyue.charging.service.INoticeInfoService;
import cn.lingyue.charging.service.ISendMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
/**
* 小程序消息模板推送
* created by king on 2020/6/18 6:08 下午
*/
@Slf4j
@Component
public class MiniSendMsgServiceImpl implements ISendMsgService {
@Autowired
private INoticeInfoService noticeInfoService;
private static MiniSendMsgServiceImpl miniSendMsgService ;
@PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
public void init() {
miniSendMsgService = this;
miniSendMsgService.noticeInfoService = this.noticeInfoService;
// 初使化时将已静态化的testService实例化
}
@Override
public boolean sendMsg(Object object) {
log.info("object:"+object);
return miniSendMsgService.noticeInfoService.mouldSend((NoticeRequest)object);
}
}
3、构建不同策略模式,添加数据
package cn.lingyue.charging.strategy;
import cn.lingyue.charging.common.enums.SendMsgChannel;
import cn.lingyue.charging.service.ISendMsgService;
import cn.lingyue.charging.service.impl.AppSendMsgServiceImpl;
import cn.lingyue.charging.service.impl.MiniSendMsgServiceImpl;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* created by king on 2020/6/18 9:19 下午
*/
@Component
public class SendMsgStrategyFactory {
private static SendMsgStrategyFactory sendMsgStrategyFactory = new SendMsgStrategyFactory();
private SendMsgStrategyFactory(){}
private static Map<String, ISendMsgService> strategyMap = new HashMap<>();
static {
strategyMap.put(SendMsgChannel.MINICHANNEL.getCode(),new MiniSendMsgServiceImpl());
strategyMap.put(SendMsgChannel.APPCHANNEL.getCode(),new AppSendMsgServiceImpl());
}
public static SendMsgStrategyFactory getInstance(){
return sendMsgStrategyFactory;
}
public ISendMsgService getSendMsgService(String sendMsgChannel){
return strategyMap.get(sendMsgChannel);
}
}
4、添加枚举类
package cn.lingyue.charging.common.enums;
import lombok.Getter;
/**
* created by king on 2020/6/18 9:22 下午
*/
@Getter
public enum SendMsgChannel {
MINICHANNEL("1","小程序消息推送"),
APPCHANNEL("2","APP消息推送"),
MINIANDAPPCHANNEL("3","小程序和APP全推送");
private String code;
private String msg;
SendMsgChannel(String code, String msg) {
this.code = code;
this.msg = msg;
}
}
5、使用test类调用测试即可
ISendMsgService sendMsgService = sendMsgStrategyFactory.getSendMsgService(
SendMsgChannel.MINICHANNEL.getCode());
boolean b = sendMsgService.sendMsg(noticeRequest);
至此简单的工厂模式+策略模式 替换if else判断就写完了,如果后期还需要添加其他渠道,只要在枚举类中添加一条数据,然后在策略模式中添加一条封装代码数据即可~~