Android MVP pattern issue

I was doing R&D in MVP, and I am thinking of using this design template for my next project. But I ran into a problem with this design pattern.

Please see below java code.

I have a BaseActivity class

public class BaseActivity extends AppCompatActivity {

}

BaseView Interface

public interface BaseView {

void showLoader();
void hideLoader();

}

Another interface that extends the BaseView interface to maintain communication between views

//Game start from here
public interface TestView extends BaseView {
    void showResult();
}

Here is the final operation:

public class MyTestActivity extends BaseActivity implements TestView {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_test);
     }

    @Override
    public void showLoader() {

    }

    @Override
    public void hideLoader() {

    }

    @Override
    public void showResult() {

    }
}

The two showLoader () and HideLoader () methods from BaseView are common to each interface that extends BaseView. so I keep them in BaseView. no problem so far.

The problem is this: I have to implement and define the providers of these methods in each class that implements BaseView or its child interface.

Example: here in

TestActivity extends BaseActivity implements TestView 

, , BaseView BaseActivity . , BaseView TestActivity BaseActivity ( BaseView BaseActivity)

TestView, BaseView. TestView BaseView. BaseView BaseActivity, showLoader() hideLoader() Activity. 2 BaseView, ...

, .

+4
2

BaseActivity. , childs, .

BaseActivity implements BaseView
+2

MVP .

BaseView BasePresenter. , , BaseView BasePresenter, :

ExampleActivity

public class ExampleActivity extends BaseActivity implements ExampleContract.View {
     private ExampleContract.Presenter presenter;

     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_example);

         presenter = new ExamplePresenter(this);
         presenter.start();
    }

    @Override
    public void setPresenter(ExampleContract.Presenter presenter) {
         this.presenter = presenter;
    }
}

ExamplePresenter

public class ExamplePresenter implements ExampleContact.Presenter {
     private ExampleContract.View exampleView;

     public ExamplePresenter(ExampleContract.View exampleView) {
         this.exampleView = exampleView;
         exampleView.setPresenter(this)
     }

     @Override
     public void start() {
         //do nothing for now
     }
}

ExampleContract

public interface ExampleContract {

    interface View extends BaseView<Presenter> {

    }

    interface Presenter extends BasePresenter {

    }

}

BaseView

public interface BaseView<T> {
    void setPresenter(T presenter);
}

BasePresenter

public interface BasePresenter {
    void start();
} 

, ?

  • , ( ), setPresenter(T presenter), start() BaseView BasePresenter.
  • View Presenter ( , , ).
  • , - onCreate, Presenter , . . , , View, Presenter.
+1

Source: https://habr.com/ru/post/1693355/


All Articles