SpringBoot 配置抽象接口

在程序设计过程中,设计了一个接口,但实现这个接口的子类并不需要实现接口中的全部方法,对于某些子类是多余的,我们不得不浪费的写上一个空的实现。

抽象接口

什么是“抽象接口”。所谓“抽象接口”,即在提供接口的同时,提供一个抽象类,用抽象类实现该接口(实际上这是缺省适配模式)。

WeChartServiceFactory 顶级接口

public interface WeChartServiceFactory {
    
}

1. WeChatService 微信基础业务

//基础业务接口
public interface WeChatService extends WeChartServiceFactory  {
	String getAccessToken(); //获取token
	String getSignature(); //获取签名
}

@Service
public class WeChartServiceImpl implements WeChartService {
    @Override
    public String getAccessToken(){
        System.out.println("wx get accessToken success");
        return "token";
    }
    @Override
    public String getSignature(){
        System.out.println("wx get signature success");
        return "signature";
    }
}

2. WeChatPayService 微信支付业务

//pay
public interface WeChartPayService extends WeChartServiceFactory {
    public String paySuccess(int id);//支付成功
    public boolean payFail(int id);//支付失败
}

@Service
public class WeChartPayServiceImpl implements WeChartPayService{
    @Override
    public boolean paySuccess(int id) {
        System.out.println("wx pay success");
        return true;
    }
    @Override
    public boolean payFail(int id) {
        System.out.println("wx pay fail");
        return true;
    }
}

3. WeChatNotifyService 微信通知业务

public interface WeChatNotifyService extends WeChartServiceFactory{
	String sendNotify();
}

@Service
public class WeChatNotifyServiceImpl extends WeChatNotifyService{
    //也可以找出子接口需要的方法,定义成抽象方法,交由子类实现
	//public abstract String getAccessToken();
	//public abstract String getSignature();
	
    @Override
    public boolean notifyToUser(int id){
        System.out.println("wx notify user and update msg status success");
        return true;
    }
}

Controller 业务调用

@RestController
public class WeChartController {
    @Resource private WeChartService weChartService;
    @Resource private WeChartPayService weChartPayService;
    @Resource private WeChartNotifyService weChartNotifyService;
    //
    @RequestMapping("/toPay")
    public Object toPay(String orderNo) {
        weChartService.getAccessToken();
        weChartService.getSignature();
        //
        weChartPayService.paySuccess(1);
        weChartPayService.payFail(0);
        //
        weChartNotifyService.notifyToUser(1);
        return Rsp.end("SUCCESS");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值