Hello, I am writing an application for android. The essence of the application: 1) the user’s window displays certain information, 2) This information is updated at regular intervals by a timer 3) During the update, a request is sent to another device, from which the answer comes later. 4) The answer is processed and the information on the screen of the android device is updated.

I would like to implement this application in accordance with the MVP pattern, i.e. View <-> Presenter <-> Model. I am still new to programming, so I would like to clarify my algorithm:

There is a main loop in which updates occur:

class ParameterRepresenter { private ParameterInteractor mParInteract; private ParameterView mParView; public ParameterRepresenter(ParameterView parView){ mParView = parView; // Первая итерация через 1 с. Timer mNewTimer = new Timer(); MainTimerTask mMainTimerTask = new MainTimerTask(); mNewTimer.schedule( mMainTimerTask, 1000 ); } class MainTimerTask extends TimerPresenterTask{ @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { //Здесь будет реализована группа Presenter updateRadioButtonStar() // далее обновление остальных параметров updateTextView1(); updateTextView2(); ...... // Следующая итерация через 1 с Timer mNewTimer = new Timer(); MainTimerTask mMainTimerTask = new MainTimerTask(); mNewTimer.schedule( mMainTimerTask, 1000 ); } }); // Разберу подробно только для одного объекта View // Например RadioButtonStar остальные элементы по // аналогии public void updateRadioButtonStar(){ if ( mParInteract.isRadioButtonStarChecked() ){ mParView.checkRadioButtonStar(); } } } } } 

Further implementation of the View group:

 class ParameterView extends AppCompatActivity(){ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // Здесь какой-то код инициализации // Создаем объект презентера, начинаем обновление // параметров mParPresenter = new ParameterPresenter(this); } public void checkRadioButtonStar(){ mRadioButtonStar.setEnabled(true); } public void uncheckRadioButtonStar(){ mRadioButtonStar.setEnabled(false); } ... // Остальные элементы } 

Next, the model group:

class ParameterInteractor () {

  public boolean isRadioButtonStarChecked(){ ... // Здесь происходит запрос, получение ответа // от другого устройства в виде обновленного // параметра и выносится решение // включать radioButtonStar или нет return ... } 

}

Is it possible in this case to put the inner class MainTimerTask (that is, the actual data update cycle itself) in the presentation from the MVP point of view? Or is it ( MainTimerTask ) better placed in the interactor?

Thanks in advance to everyone for the answers.

    1 answer 1

    Here the main problem is that everyone understands the MVP pattern in its own way. In any case, you must be guided by reasonable necessity.

    In your case, I (subjective opinion) would place it in a separate class with the name of something like ModelController and update Model with it.

    Threat But it must be a mistake

     class ParameterView(){ 

    wangoo here for example

     class ParameterView extends AppCompatActivity{ 
    • Thanks for the answer. As for the error, you correctly noted that it was correct to declare a ParameterView with inheritance. As for the presenter: you don’t like the name ParameterRepresenter, so do you recommend ModelController ? Because in my ParameterRepresenter class, the same thing as you propose is done, the model is updated through the ParameterRepresenter intermediary. - foxis
    • one
      @foxis This is everyone's personal preferences. The main rule when choosing a name is that it can be understood from what the object is doing. ModelController controls the model, it will be clear to the new person in general what he is doing. ParameterRepresenter - represents the parameters. But I repeat - this is all very subjective and individual. - rjhdby
    • An approximate picture of the construction of MVP for my case is clear, Thank you very much for your comprehensive answers. - foxis
    • one
      Only the presenter can control the model (like the view) in MVP, no matter how someone understands this pattern. And, in general, where does the model? Here, the model is not updated, but View. Initially, everything is properly organized, i.e. the data should be received by the presenter from the model (via ParameterInteractor), and the methods updateRadioButtonStar, updateTextView1, etc. should be implemented in View and in presenter called as (example) mParView.updateRadioButtonStar (). - mit
    • @mit, thanks a lot for the answer. - foxis