简体   繁体   English

Android中的MVP设计模式问题

[英]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. 我在MVP上做研发,我正在考虑将这个设计模式用于我的下一个项目。 But I am facing a problem with this design pattern. 但我面临着这种设计模式的问题。

Please have a look at below java code. 请看下面的java代码。

I have a BaseActivity class 我有一个BaseActivity类

public class BaseActivity extends AppCompatActivity {

}

An Interface BaseView 接口BaseView

public interface BaseView {

void showLoader();
void hideLoader();

}

One more interface which extends BaseView Interface to maintain relation between views 另外一个接口扩展了BaseView接口以维护视图之间的关系

//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. 来自BaseView的2个方法showLoader()和HideLoader()对于扩展BaseView的每个接口都是通用的。 that's why I keep them into BaseView. 这就是我将它们保留在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. 问题是:我必须在实现BaseView或其Child接口的每个类中实现和提供这些方法的定义。

Example: Here in 示例:此处

TestActivity extends BaseActivity implements TestView 

So to prevent this problem I implemented BaseView into BaseActivity and provider these methods definition. 因此,为了防止这个问题,我将BaseView实现为BaseActivity并提供这些方法定义。 But I can see BaseView is coming to TestActivity from BaseActivity(if I implement BaseView in BaseActivity) 但我可以看到BaseView从BaseActivity转向TestActivity(如果我在BaseActivity中实现BaseView)

And Also from TestView which is already implementing BaseView. 而且还来自已经实现BaseView的TestView。 It's my requirement TestView must extend BaseView. 这是我的要求TestView必须扩展BaseView。 if i do not implements BaseView in BaseActivity i need to implements showLoader() and hideLoader() methods in every Activity class. 如果我没有在BaseActivity中实现BaseView,我需要在每个Activity类中实现showLoader()和hideLoader()方法。 right now I have 2 methods in BaseView they can be more... 现在我在BaseView中有2个方法,他们可以更多...

Please suggest any solution. 请建议任何解决方案。

Just implement default implementation in BaseActivity. 只需在BaseActivity中实现默认实现。 So You can override methods in childs if needs. 因此,如果需要,您可以覆盖子项中的方法。

BaseActivity implements BaseView

How I would implement MVP is as follows. 我如何实现MVP如下。

You'd have your BaseView and BasePresenter. 你有BaseView和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: 那么你的View和Presenter之间有一个合约,它有自己的扩展你的BaseView和BasePresenter的接口,我将告诉你如下:

ExampleActivity 为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 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 ExampleContract

public interface ExampleContract {

    interface View extends BaseView<Presenter> {

    }

    interface Presenter extends BasePresenter {

    }

}

BaseView 基本视点

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

BasePresenter 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 . 因此,如果您想要所有合同 (而不是视图)的共享实现,即我的每个视图和演示者都要在整个应用程序中调用setPresenter(T presenter)start()调用,请将其添加到BaseViewBasePresenter
  • 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. 然后在您的合同中,您的View界面和Presenter界面(各个活动将实现的界面)将使您自己的功能与您要实现的活动相对应。
  • 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. 然后,Presenter通过将自身传递到onCreate的presenters构造函数链接到视图,然后在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. 您现在要做的就是在View界面和Presenter界面中将方法添加到您的合同中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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