Model-View-Presenter (MVP) for Android
We take a look at how to build apps and web apps for this popular mobile OS by using the Model-View-Presenter (MVP) pattern.
Join the DZone community and get the full member experience.
Join For FreeIn this blog, we are going discuss the MVP Design Pattern for Android which is a better and modified alternate to MVC.
Note: I am trying to make this (MVP) concept as easy as possible because everyone might have different opinions on MVP. So, do not get confused because it is simply a design pattern to write our code in a more readable and segregated manner. The main concepts we are going to learn are how the Model, View, and Presenter are interlinked and to get familiar with this. You can implement this design pattern in your own way.
What Is MVP?
MVP is a design pattern for developers to write their code in a more readable, maintainable, and
scalable manner. In MVP, our code is divided into three parts named Model, View, and Presenter, rather than placing the whole code in one activity.
1. Model
Everything which is related with data is a part of the Model. The Model contains a data provider and the code which fetches and updates the data. This part of MVP updates the database or communicates with a web server.
2. Presenter
The Presenter contains the application's business logic and when an operation is performed or data is changed then it will notify the View that it needs to update.
3. View
The View part of MVP contains the visual parts of our application, like showing messages, and also handles visibility. View contains only that part of the code which is related to UI and it does not contain any logic related to the displayed data. The View is controlled by the Presenter.
Why Use MVP?
This MVP design pattern helps to segregate code into three different parts: business logic (Presenter); UI (View); data interaction (Model). This modulation of code is easy to understand and maintain.
For example: In our application, if we use a content provider to persist our data and later we want to upgrade it with a SQLite database, the MVP design pattern will make this very easy.
How to Implement MVP for Android:
A simple example of a user login made with the MVP design Pattern.
/*The Interface LoginPresenter.*/
public interface LoginPresenter
{
/*when user click on login button from Activity*/
void handleLogin(String username, String password);
}
/*The Interface LoginView.*/
public interface LoginView
{
void showValidationErrorMsg();
void loginSuccessFully();
void loginFail();
}
/* Class LoginPresenterImpl.*/
public class LoginPresenterImpl implements LoginPresenter
{
private LoginView loginView;
public LoginPresenterImpl(LoginView loginView)
{
this.loginView = loginView;
}
@Override
public void handleLogin(String username, String password)
{
if ((TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
{
loginView.showValidationErrorMsg();
}
else
{
if (username.equals("Standerd") && password.equals("Standerd"))
{
loginView.loginSuccessFully();
}
else
{
loginView.loginFail();
}
}
}
}
/* Main Activity Class.*/
public class MainActivity extends AppCompatActivity implements LoginView
{
private LoginPresenter presenter;
private TextView textViewUserName;
private TextView textViewPassword;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeView();
presenter = new LoginPresenterImpl(this);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presenter.login(textViewUserName.getText().toString(), textViewPassword.getText().toString());
}
});
}
private void initializeView()
{
textViewUserName = findViewById(R.id.textViewUserName);
textViewPassword = findViewById(R.id.textViewPassword);
buttonLogin = findViewById(R.id.buttonLogin);
}
@Override
public void showValidationErrorMsg()
{
Toast.makeText(this, "Username or Password is incorrect", Toast.LENGTH_SHORT).show();
}
@Override
public void loginSuccessFully()
{
Toast.makeText(this, "Login SuccessFully", Toast.LENGTH_SHORT).show();
}
@Override
public void loginFail()
{
Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
}
Conclusion
In Android, it is not easy to separate the interface from the logic, but the MVP design pattern makes it easier to prevent activities which may end up degrading into coupled classes. In big applicaitons, it is important to organize and manage the code which makes the applications easy to maintain and extend.
Published at DZone with permission of Abhishek Tripathi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments