-
Model View Presenter (MVP)Modeling/DesignPattern 2020. 4. 10. 23:57
1. Overview
2. Description
2.1 Model
In an application with good layered architecture, this model would only be the gateway to the domain layer or business logic. See it as the provider of the data we want to display in the view. Model’s responsibilities include using APIs, caching data, managing databases and so on.
2.2 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.
2.3 Presenter
The Presenter is responsible to act as the middle man 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.
3. Example
3.1 Presenter
public class MainActivityPresenter { private User user; private View view; public MainActivityPresenter(View view) { this.user = new User(); this.view = view; } public void updateFullName(String fullName){ user.setFullName(fullName); view.updateUserInfoTextView(user.toString()); } public void updateEmail(String email){ user.setEmail(email); view.updateUserInfoTextView(user.toString()); } public interface View{ void updateUserInfoTextView(String info); void showProgressBar(); void hideProgressBar(); } }
3.2 Model
/** * Created by bpn on 11/30/17. */ public class User { private String fullName = "", email = ""; public User() { } public User(String fullName, String email) { this.fullName = fullName; this.email = email; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Email : " + email + "\nFullName : " + fullName; } }
3.3 Activity
public class MainActivity extends AppCompatActivity implements MainActivityPresenter.View { private MainActivityPresenter presenter; private TextView myTextView; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); presenter = new MainActivityPresenter(this); myTextView = findViewById(R.id.myTextView); EditText userName = findViewById(R.id.username); EditText email = findViewById(R.id.email); initProgressBar(); userName.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { presenter.updateFullName(s.toString()); } @Override public void afterTextChanged(Editable s) { hideProgressBar(); } }); email.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { presenter.updateEmail(s.toString()); } @Override public void afterTextChanged(Editable s) { hideProgressBar(); } }); } private void initProgressBar() { progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleSmall); progressBar.setIndeterminate(true); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Resources.getSystem().getDisplayMetrics().widthPixels, 250); params.addRule(RelativeLayout.CENTER_IN_PARENT); this.addContentView(progressBar, params); showProgressBar(); } @Override public void updateUserInfoTextView(String info) { myTextView.setText(info); } @Override public void showProgressBar() { progressBar.setVisibility(View.VISIBLE); } @Override public void hideProgressBar() { progressBar.setVisibility(View.INVISIBLE); } }
4. Dependency
5. Difference between MVC and MVP
5.1 Model View Controller (MVC)
- Controllers are behavior-based and can share multiple views.
- View can communicate directly with Model
5.2 Model View Presenter (MVP)
- View more separated 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
- Listen to user action and model updates
- Updates model and view as well
6. Reference
https://medium.com/cr8resume/make-you-hand-dirty-with-mvp-model-view-presenter-eab5b5c16e42
https://medium.com/@tinmegali/model-view-presenter-mvp-in-android-part-2-a15e35891095
https://medium.com/@anshul.vyas380/model-view-presenter-b7ece803203c
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter
'Modeling > DesignPattern' 카테고리의 다른 글
Flux (0) 2020.04.11 Mediator (0) 2020.03.01 Memento (0) 2020.03.01 Composite (0) 2020.02.29 Chain of responsibility (0) 2020.02.29