Model View Presenter

Anshul vyas
3 min readOct 7, 2018

--

The Model View Presenter (M.V.P.) is a design paradigm, which is architecturally the derivation of MVC pattern, is generally used for building user interfaces. MVP in popular design pattern adopted in android programming where the presenter becomes the middleman containing UI business logic for the View. MVP advocates separating business and persistence logic out of the Activity and Fragment

The model-view-presenter software pattern originated in the early 1990s at Taligent, a joint venture of Apple, IBM, and Hewlett-Packard. It was further popularized by Dolphin Smalltalk in1998, then in 2006, Microsoft adopted MVP for user interface programming in the .NET framework.

MVP has emerged as a user interface architectural pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic.

The typical interactions taking place in MVP architecture can be understood through the following

Model View Presenter Interactions

Model

The model is an interface defining the data to be displayed or otherwise acted upon in the user interface. Model is the data layer which is responsible for handling the business logic and for the communication with the network and database layers. Model’s responsibilities include using APIs, caching data, managing databases and so on.

/*The UserModel Class*/public class UserModel{
private SharedPreference userPref;
private UserMode(){
// init data interaction handler
}
public boolean isValidUser(String username, String Password){
// data interaction logic
}
}

View

The View, usually implemented by an Activity, will contain a reference to the presenter. The only thing that the view will do is to call a method from the Presenter every time there is an interface action. It is only responsible for presenting data in a way decided by the presenter. The functionality of view is typically reduced to a minimum, and it’s made as a passive component, by moving the business logic to the presenter. The view is also shielded from the model by delegating all the interaction to happen through the middleman presenter.

/*The Interface LoginView.*/public interface LoginView{void loginSuccessFully();void loginFail();}/* Main Activity Class.*/public class MainActivity extends AppCompatActivity implements LoginView{private LoginPresenter presenter;private TextView textViewUserName;private TextView textViewPassword;private Button buttonLogin;@Overrideprotected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);initializeView();presenter = new LoginPresenter(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);}@Overridepublic void loginSuccessFully(){Toast.makeText(this, "Login SuccessFully", Toast.LENGTH_SHORT).show();}@Overridepublic void loginFail(){Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();}}

Presenter

The Presenter is responsible to act as the middleman between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View. Due to this, the view and presenter work closely together, they need to have a reference to one another. Thus, the relationship between the Presenter and its corresponding View is defined in an interface. The Presenter is also decoupled directly from the View and talks to it through this interface. This is to allow mocking of the View and Presenter in a unit test.

/* Class LoginPresenter */public class LoginPresenter{private LoginView loginView;
private Usermodel userModel;
public LoginPresenter(LoginView loginView){this.loginView = loginView;
this.userModel = new UserModel();
}public void handleLogin(String username, String password){if(userModel.isValidUser(username, password)){
loginView.loginSuccessFully();
}
else{
loginView.loginFail();
}
}

MVC vs MVP

Model View Presenter

  • View more separate from Model. The Presenter is the mediator between Model and View.
  • Easier to create unit tests
  • Generally, there is a one to one mapping between View and Presenter, with the possibility to use multiple presenters for complex views

Model View Controller

  • Controllers are behaviour based and can share multiple views.
  • The view can communicate directly with the Model.

--

--

Anshul vyas

Product Engineer @ GO-JEK Tech | History and Literature Enthusiast |IIITIAN | Nerd | Music Lover |