利用工厂模式+策略模式替换if else判断案例演示

基于业务场景,我需要使用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.annotatio
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值