Java – MVP design pattern issues in Android

MVP design pattern issues in Android… here is a solution to the problem.

MVP design pattern issues in Android

I

was working on MVP and I wanted to use this design pattern for my next project.
But I ran into a problem with this design pattern.

Take a look at the Java code below.

I have a BaseActivity class

public class BaseActivity extends AppCompatActivity {

}

An interface, BaseView

public interface BaseView {

void showLoader();
void hideLoader();

}

An additional interface extends the BaseView interface to maintain relationships between views

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

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

}
}

The 2 methods of BaseView, showLoader() and HideLoader(), are common to each interface that extends BaseView.
That’s why I keep them in BaseView. No problem until now.

The problem is: I have to implement and provide definitions of these methods in every class that implements a BaseView or its subinterfaces.

Example: Here

TestActivity extends BaseActivity implements TestView 

So to prevent this problem, I implemented the BaseView into BaseActivity and provided these method definitions.
But I can see that the BaseView is going from BaseActivity to TestActivity (if I implement BaseView in BaseActivity).

There is also TestView from BaseView, which has already implemented BaseView.
My requirement is that TestView must extend BaseView.
If I don’t implement BaseView in BaseActivity, I need to implement showLoader() and hideLoader() methods in each Activity class. Now I have 2 methods in BaseView and they can be more….

Please suggest any solutions.

Solution

Just implement the default implementation in BaseActivity. Therefore, you can override the methods in the child object if needed.

BaseActivity implements BaseView

Related Problems and Solutions