In MVP is onClick responsibility of View or Presenter?

后端 未结 2 1301
旧时难觅i
旧时难觅i 2020-12-14 17:59

In the MVP pattern who is responsible to handle clicks on the UI?
E.g. the non-MVP approach would be something like:

counterButton.setOnClickListener(n         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 18:47

    In MVP, it is the responsibility of the View to know how to capture the click, not to decide what to do on it. As soon as the View captures the click, it must call the relevant method in the Presenter to act upon it:

    ------------------- View --------------------

    button1.setOnClickListener(new OnClickListener({
    presenter.doWhenButton1isClicked();
    }));
    

    ------------------ Presenter ----------------

    public void doWhenButton1isClicked(){
    // do whatever business requires
    }
    

    I have a series of articles on architectural patterns in android, part 3 of which is about MVP. You might find it helpful.

提交回复
热议问题