Skip to content

Commit

Permalink
Fixed syntax highliting
Browse files Browse the repository at this point in the history
  • Loading branch information
sockeqwe committed Jan 11, 2017
1 parent f79fad9 commit ff99bd6
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions _posts/2017-01-09-mosby3-mvi-1.md
Expand Up @@ -17,7 +17,7 @@ So what do I mean with "modeled Models in a wrong way"? Well, there are a lot of

Example: Just load a list of persons from a backend. A "traditional" MVP implementation could look like this:

```java
{% highlight java %}
class PersonsPresenter extends Presenter<PersonsView> {

public void load(){
Expand All @@ -34,13 +34,13 @@ class PersonsPresenter extends Presenter<PersonsView> {
});
}
}
```
{% endhighlight %}

But where or what is the "Model"? Is it the backend? No, thats business logic. Is it the result List<Person>? No, that is just one thing our View displays amongst others like a loading indicator or an error message. So what actually is the "Model"?

In my opinion, there should be a "Model" class like this:

```java
{% highlight java %}
class PersonsModel {
// In a real world application those fields would be private
// and we would have getters to access them
Expand All @@ -54,11 +54,11 @@ class PersonsModel {
this.error = error;
}
}
```
{% endhighlight %}

And then the Presenter could be implemented like this:

```java
{% highlight java %}
class PersonsPresenter extends Presenter<PersonsView> {

public void load(){
Expand All @@ -75,7 +75,7 @@ class PersonsPresenter extends Presenter<PersonsView> {
});
}
}
```
{% endhighlight %}

Now the View has its own Model which then will be "rendered" on the screen. This concept is not really something new. The original MVC definition by Trygve Reenskaug from 1979 had quite a similar concept: The View observes the Model for changes. Unfortunately, the term MVC has been (mis)used to describe too many different patterns that are not really the same what Reenskaug formulated in 1979. For instance backend developers use MVC frameworks, iOS has ViewController and what does MVC on Android actually mean? Activities are Controller? What is a ClickListener then? Nowadays the term MVC is just a big mistake, misusage and misinterpretation of what Reenskaug originally formulated. But let's stop this discussion about MVC here, this could run out of control.

Expand All @@ -95,7 +95,7 @@ Let's discuss this points and let's see how "traditional" implementations of MVP
Reactive Apps - this is a buzzword, isn't it? With that I mean apps with a UI that react on state changes. Ah, here we have another nice word: "State". What is "State"? Well, most of the time we describe "state" as what we see on the screen like "loading state" when the View displays a ProgressBar. Therein lies the crux: we frontend developers tend to be focused on UI. That is not necessarily a bad thing because at the end of the day a good UI decides whether or not a User will use our app and therefore how successful an app is. But take a look at the very basic MVP code example from above (not the one using PersonsModel).
Here the state of the UI is coordinated by the Presenter. The Presenter tells the view what to display. The presenter drives the state of the UI. The same is true for MVVM. In this blog post I want to distinguish between two MVVM implementations: The first one with android's data binding and the second option using RxJava. In MVVM with data binding the state directly sits in the ViewModel:

```java
{% highlight java %}
class PersonsViewModel {
ObservableBoolean loading;
// ... Other fields left out for better readability
Expand All @@ -117,11 +117,11 @@ class PersonsViewModel {
});
}
}
```
{% endhighlight %}

In MVVM with RxJava we don't use the data binding engine but bind Observable to UI Widgets in the view, for example:

```java
{% highlight java %}
class RxPersonsViewModel {
private PublishSubject<Boolean> loading;
private PublishSubject<List<Person> persons;
Expand Down Expand Up @@ -150,7 +150,7 @@ class RxPersonsViewModel {
return loadPersonsCommand;
}
}
```
{% endhighlight %}

Of course this code snippets are not perfect and your implementation may look entirely different. The point is that usually in MVP and MVVM the state is driven by either the Presenter or ViewModel.

Expand All @@ -169,7 +169,7 @@ In worst case, you have a serious bug reported to you from a crash reporting too

What if we only have one single source of truth for state passed from bottom (business logic) to the top (the view). Actually, we have already showcased a similar concept at the very beginning of this blog post when we talked about "Model".

```java
{% highlight java %}
class PersonsModel {
// In a real world application those fields would be private
// and we would have getters to access them
Expand All @@ -183,7 +183,7 @@ class PersonsModel {
this.error = error;
}
}
```
{% endhighlight %}

Guess what? **Model reflects the State**. Once I have understood this, a lot of state related issues are solved (and prevented from the very beginning) and suddenly my Presenter has exactly one output: _getView().render(PersonsModel)_. This reflects a simple mathematical function like _f(x) = y_ (also possible with multiple inputs i.e. f(a,b,c), exactly one output). Math might not be everyone's cup of tea but a mathematician doesn't know what a bug is. Developers do.

Expand Down

0 comments on commit ff99bd6

Please sign in to comment.