#3 ~ Android MVP Design Pattern

raditya gumay
3 min readDec 4, 2016

--

Android MVP Image taken from (https://caster.io/sign-up)

Currently MVP and MVVM is hottest news among Android Software Engineer besides SOLID or CLEAN Architecture, RxJava, Dagger2, Kotlin and others.

MVP is a part of big 4 Design Pattern in way to create a an application. Such as MVP, MVVM, MVC, and VIPER (IOS).

Before we dive in MVP Design Pattern, we should have a questions in our mind. Why should we consider using a Pattern? its a good approach? and bla bla bla.. Tons of questions maybe appear in “newbie” engineer mind.

This will answer you questions about Design Pattern.

Design Patterns simply addresses this issue and makes a common platform for all developers. We shall remember that these patterns were intended to be applied in object-oriented designs with the intention of reuse

Above quote generally for OOP Design Pattern, like Factory Pattern, Singleton Pattern, Proxy Pattern, Facade Pattern, and etc. But, we can consider MVP as a same approach but more higher. We can says MVP, MVVM, MVC a base for building an application, and others OOP are pole for that building.

MVP has a base for building an application, which we could say, Design pattern is very basic knowledge to build an application. You can read this article to extend you knowlegde Understanding The Difference Between MVC, MVP and MVVM Design Patterns

MVP Stands for:

Model :
This represent our model data, whatever coming from database, file, api, preference, and etc.

View:
View represent our activity or fragment or dialog, anything users see, we can says that our view

Presenter:
Presenter represent our business logic (for a simple sentence). But, presenter completely separated with View. (Android MVP does not use a basic callback [some people says listener], we use a base observer pattern) (we need a clean code)

You might do not know, what difference between a callback and an observer. Technically speaking, a listener will carefully anonymously (function) listen you event. An observer will listen once you registered it and unlisten once you unregistered.

Image taken from http://www.techyourchance.com/wp-content/uploads/2015/06/MVC_MVP.png

Let’s dive in code (Someone happy for this)
I Expect you have knowledge write code in Android, or Java generally.

In Activity

public class MyActivity extends BaseActivity implements
MyActivityView {

private static final String TAG = MyActivity.class.getSimpleName();

private MyActivityPresenter mPresenter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mPresenter = new MyActivityPresenterImpl(this, this);
mPresenter.doSomeWork();
}@Override
public void onSomeWorkResult(@NonNull String str){
((TextView).findViewById(R.id.textview)).setText(str);
}

Explanation:
You may have a question about this line MyActivityPresenterImpl this is represent our business logic. Then what is MyActivityPresenter? This presenter just an interface for invoke our business logic to view. (But, wait you say Presenter is our Business Logic, and now it just an interface?). In general theory MVP we not include or Impl (interactor) in our MVP design pattern. But, for technically P (Presenter have their interface).
After we initialize an interactor, we call our business logic doSomeWork();

In View

public interface MyActivityView {
void onSomeWorkResult(@NonNull String str);
}

Explanation:
We invoke our view from business logic using this implementation.

In Presenter

public interface MyActivityPresenter {
void doSomeWork();
}

Explanation:
We invoke our business logic from view using this implementation.

In PresenterImpl

public void doSomeWork(){
Observable.just("Hell world").subscribe(new Action1<String>() {
@Override
public void call(String str) {
mView.onSomeWorkResult(str);
}
});
}

Note: I use RxJava 2.x

Source Code:
I am using kotlin in this sample.

Hope you enjoy with this article. for further info, just comment or email me at gumay[dot]raditya[at]gmail[dot]com.

@r_adiit | Github | Linkedin

--

--