我尝试通过策略模式创建付款。但我读过的所有文章都是这样的:
public interface PayStrategy {
void pay(BigDecimal paymentAmount);
}
但是如果我需要返回Single<RestResponse<PaymentResponse>>
?这是正确的方法吗?
public interface PayStrategy {
Single<RestResponse<PaymentResponse>> pay(BigDecimal paymentAmount);
}
在任何真实的系统中,支付请求都会返回结果
最佳答案
我建议您在通用返回类型中实现问题陈述,如下所示:
public interface IPayStrategy<T> {
T Pay();
}
public class PayStrategy1 :IPayStrategy<int> {
public int Pay() { }
}
public class PayStrategy2 :IPayStrategy<String> {
public String Pay() { .. }
}
public class Context<T> {
private IPayStrategy<T> payStrategy;
public setStrategy(IPayStrategy<T> strategy) { this.payStrategy = strategy; }
public T doPayment() {
return payStrategy.Pay();
}
}
关于java - 有返回值的策略模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49027772/