简体   繁体   中英

MVP design pattern issue in Android

I was doing R&D on MVP and I am thinking to use this design pattern for my next project. But I am facing a problem with this design pattern.

Please have a look at below java code.

I have a BaseActivity class

public class BaseActivity extends AppCompatActivity {

}

An Interface BaseView

public interface BaseView {

void showLoader();
void hideLoader();

}

One more interface which extends BaseView Interface to maintain relation between views

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

Here is the final Activity :

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() {

    }
}

2 methods showLoader() and HideLoader() from BaseView are common to every interface which extends BaseView. that's why I keep them into BaseView. no problem till now.

Problem is: I have to implements and provider definition of these methods in every class which implements BaseView or its Child interface.

Example: Here in

TestActivity extends BaseActivity implements TestView 

So to prevent this problem I implemented BaseView into BaseActivity and provider these methods definition. But I can see BaseView is coming to TestActivity from BaseActivity(if I implement BaseView in BaseActivity)

And Also from TestView which is already implementing BaseView. It's my requirement TestView must extend BaseView. if i do not implements BaseView in BaseActivity i need to implements showLoader() and hideLoader() methods in every Activity class. right now I have 2 methods in BaseView they can be more...

Please suggest any solution.

Just implement default implementation in BaseActivity. So You can override methods in childs if needs.

BaseActivity implements BaseView

How I would implement MVP is as follows.

You'd have your BaseView and BasePresenter. Then you'd have a contract between your View and Presenter which has it's own interfaces that extends your BaseView and BasePresenter, i'll show you as follows:

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();
} 

So what can we take away from this and how does it all link up?

  • So, if you want shared implementation for all your contracts (not views) ie every one of my views and presenters will either have a setPresenter(T presenter) call and start() call throughout the whole app add it to your BaseView or BasePresenter .
  • Then in your contracts your View interface and Presenter interface (which the individual activities will be implementing, will then have it's own functionality individual to the activity you want to implement.
  • Then, the Presenter is linked to the view by passing itself into the presenters constructor in onCreate and then inside the Presenter's constructor it'll callback to the view by setting the presenter. They are both now assigned to each other through a binding contract. All you have to do now is add the methods to your contract either in the View interface and the Presenter interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM