具有回报价值的策略模式

问题描述 投票:2回答:2

我尝试按策略模式创建付款。但我读过的所有文章都是这样的:

public interface PayStrategy {
    void pay(BigDecimal paymentAmount);
}

但如果我需要返回Single<RestResponse<PaymentResponse>>?这是正确的方法吗?

public interface PayStrategy {
    Single<RestResponse<PaymentResponse>> pay(BigDecimal paymentAmount);
}

在任何实际系统中,付款请求都将返回结果

java android design-patterns strategy-pattern
2个回答
2
投票

我建议你在Generic返回类型中实现你的问题陈述,如下所示:

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();
    }
}

0
投票

在我看来这是正确的,因为这取决于你对合同的定义,如果你同意所有的策略都必须为我返回Single<RestResponse<PaymentResponse>>类型的结果它是正确的

© www.soinside.com 2019 - 2024. All rights reserved.