题目
用户有多种支付方式(余额、红包、优惠券,代金券等),假如每种支付方式通过调用远程服务获取可用性。
在外部资源环境不变情况下,请设计程序以最短响应时间获得尽可能多的可用支付方式列表。
假定支付方式可用性咨询接口定义:PaymentRemoteSerivce
接口方法:ConsultResult isEnabled(String paymentType);
返回结果:
public class ConsultResult {
public ConsultResult (boolean isEnable,String errorCode){
this.isEnable = isEnable;
this.errorCode= errorCode;
}
/** 咨询结果是否可用*/
private boolean isEnable;
/** 错误码 */
private String errorCode;
public boolean getIsEnable(){
return isEnable;
}
public String getErrorCode(){
return errorCode;
}
}
解题思路
1、进行远程调用
2、远程调用后存缓存
3、发生变化通过socket或者mq进行通知,刷新缓存
4、定时刷新缓存、防止某时刻大量请求过来而没得缓存
开干
0、定义支付方式常量
package payWay.other;
/**
* 支付方式常量
* 余额、红包、优惠券,代金券等
* @author sirwsl
*/
public class Constants {
/**
* 余额
*/
public static final String BALANCE = "10";
/**
* 红包
*/
public static final String RED_ENVELOPE = "20";
/**
* 优惠券
*/
public static final String COUPONS = "30";
/**
* 代金券
*/
public static final String VOUCHERS = "40";
/**
* 其他
*/
public static final String OTHER = "50";
}
1、定义支付方式枚举值
package payWay.other;
/**
* 余额、红包、优惠券,代金券等
* @author sirwsl
*/
public enum PayWayEnum {
//性别枚举
BALANCE("余额", Constants.BALANCE),
RED_ENVELOPE("红包", Constants.RED_ENVELOPE),
COUPONS("优惠券",Constants.COUPONS),
VOUCHERS("代金券",Constants.VOUCHERS),
OTHER("其他",Constants.VOUCHERS);
private String name;
private String value;
PayWayEnum(String name, String code) {
this.name = name;
this.value = code;
}
public static PayWayEnum matchByValue(String value) {
for (PayWayEnum item : PayWayEnum.values()) {
if (item.value.equals(value)) {
return item;
}
}
return OTHER;
}
public static PayWayEnum matchByName(String name) {
for (PayWayEnum item : PayWayEnum.values()) {
if (item.name.equals(name)) {
return item;
}
}
return OTHER;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;

本文介绍了一种通过缓存和并发处理来优化支付方式可用性检查的方法,利用多线程和缓存技术减少响应时间。
最低0.47元/天 解锁文章
1435





