我正在使用访问者模式将支付处理从 android 中的 UI 代码中抽象出来。我对我应该传递给访问者构造函数的内容有一些疑问,以便视图在完成处理付款后得到回调。
让我向您展示我到目前为止所拥有的:
我正在处理 2 个支付系统,因此有两种支付策略(brainTree 和 Stripe):
public class BrainTreePaymentStrategy implements IVisitable {
@Override
public void makePayment() {
}
@Override
public void accept(Visitor v) {
}
}
public class StripePaymentStrategy implements IVisitable {
@Override
public void makePayment() {
}
@Override
public void accept(IVisitor v) {
}
}
public interface IVisitable {
void makePayment();
void accept(IVisitor v);
}
public interface IVisitor {
//list out all the classes the visitor can visit now
void visit(StripePaymentStrategy stripePaymentStrategy);
void visit(BrainTreePaymentStrategy brainTreePaymentStrategy);
}
//now critical, lets create a real concrete visitor that can actually do the work:
public class PaymentStrategyVistor implements IVisitor {
@Override
public void visit(StripePaymentStrategy stripePaymentStrategy) {
//process the braintree payment here, but how to give call back to UI ?
}
@Override
public void visit(BrainTreePaymentStrategy brainTreePaymentStrategy) {
//process the braintree payment here, but how to give call back to UI ?
}
}
我正在使用鲍勃叔叔的干净架构,所以我的网络调用是通过用例进行的,而且我使用 mvp 作为我的表示层,所以如果需要我可以访问演示者和用例。
所以我的问题再次是关于 PaymentStrategyVistor 类,如果我将演示者作为构造函数参数传递,你会怎么想。例如,我可以调用presenter.doBrainTreePayment("someToken");
我可以在visitors visit(BrainTreePaymentStrategy brainTreePaymentStrategy)
方法中执行此操作。这就是你们都会这样做的吗?