在程序设计过程中,设计了一个接口,但实现这个接口的子类并不需要实现接口中的全部方法,对于某些子类是多余的,我们不得不浪费的写上一个空的实现。
抽象接口
什么是“抽象接口”。所谓“抽象接口”,即在提供接口的同时,提供一个抽象类,用抽象类实现该接口(实际上这是缺省适配模式)。
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");
}
}