Comments on my MVP pattern for Android

前端 未结 1 1933
猫巷女王i
猫巷女王i 2020-12-14 04:31

I am planning to use MVP pattern for my new Android project. I have done some sample code and I would like to know, have I implemented it correctly? Please give comments on

相关标签:
1条回答
  • 2020-12-14 05:17

    Sorry for the late :) I've use MVP on Android this way.

    Activities are presenters. Every presenter has a link to model(s) (sometimes it is services, sometimes not, depending from the task) and to view(s). I create custom view and set it as the content view for activity.

    See:

    public class ExampleModel {
       private ExampleActivity presenter;
    
       public ExampleModel(ExampleActivity presenter) {
           this.presenter = presenter;
       }
       //domain logic and so on
    }
    
    public class ExampleActivity extends Activity {
       private ExampleModel model;
       private ExampleView view;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           model = new ExampleModel(this);
           view = new ExampleView(this);
           setContentView(view);
       }
       // different presenter methods
    }
    
    public class ExampleView extends LinearLayout {
    
        public ExampleView(Context context) {
            super(context);
        }
    }
    

    Also, I've discussed this topic here.

    I should warn you, that Activity shouldn't be considered as the view. We had very bad expirience with it, when we wrote with PureMVC which considered Activity as view component. Activity is excellently suitable for controller/presenter/view model (I've tried all of them, I like MVP the most), it has excellent instrumentation for managing the views (View, Dialog and so on) while it's not a view itself.

    0 讨论(0)
提交回复
热议问题