网络呼叫的访客模式 - 从vistor更新android UI的最佳方式

时间:2018-03-02 10:10:50

标签: android visitor-pattern clean-architecture

我正在使用访问者模式来抽象远离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 ?
    }
}

我正在使用叔叔bob的干净的架构,所以我的网络调用是通过用例,我也使用mvp为我的表示层,所以如果需要我可以访问演示者和用例。

所以我的问题再次出现在PaymentStrategyVistor类中,如果我作为构造函数参数传入presenter,你会怎么想?例如,我可以调用presenter.doBrainTreePayment("someToken");我可以在visitors visit(BrainTreePaymentStrategy brainTreePaymentStrategy)方法中执行此操作。这是你们所有人都会这样做的吗?

1 个答案:

答案 0 :(得分:1)

您的建议(将演示者传递给每位访问者的构造函数)似乎完全没问题。

相关问题