Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Common solutions for programming problems are called patterns, and in this video we will learn about the model-view-presenter pattern, or MVP for short. Android is designed to make heavy use of the MVP pattern, and knowing what it is will give us a better understanding of how our apps work and how they should be architected.
Related Links
- Model-View-Presenter Pattern (Wikipedia)
- Introduction to Model View Presenter on Android
- What are MVP and MVC and what is the difference?
- Model-View-Presenter: Android guidelines
The MVP pattern is essentially this:
- Model: What to display
- View: How it’s displayed
- Presenter: Formatting the model for display and handling events like user input
GitHub Repo
Hey, welcome back.
0:04
We're gonna take a short break from
code to talk about the popular software
0:06
design pattern called
model-view-presenter, or MVP, for short.
0:09
Now don't worry if this video
seems a little unrelated at first
0:13
to the app that we're working on.
0:16
We don't need to know all the intricacies
0:18
of the model-view-presenter
design pattern.
0:20
But the more we know about it and
0:22
keep it in mind,
the better software developers we will be.
0:24
We don't necessarily need to follow it for
smaller apps like this.
0:27
But good programming patterns and
0:31
practices are invaluable on larger
projects or when working with teams.
0:32
By the way,
in a previous version of this course,
0:37
we taught this as
the model-view-controller design pattern.
0:39
The two are often used interchangeably,
but there are subtle differences.
0:42
And the model-view-presenter
pattern more closely aligns
0:46
with how Android apps are architected.
0:49
So first off,
what is a software design pattern?
0:51
When writing code,
0:54
you'll always end up facing problems
that are common to all developers.
0:55
For example, one of those problems
is how to architect our apps so
0:59
that our components are largely reusable.
1:02
Solutions to these common problems
are known as design patterns.
1:05
And while you weren't explicitly
aware of it, we've been working with
1:08
the model-view-presenter
design pattern all along.
1:11
Let's start with a very generic example.
1:14
Think of a slideshow presentation
you have given at work or
1:16
in a class using a tool like PowerPoint,
Google Slides, or Keynote.
1:19
In this case, you are the presenter.
1:23
Your job is to control what is on each
slide and when they are displayed.
1:26
These slides are being presented on
a large screen, which is the view.
1:30
This is how the audience
views your material.
1:34
Lastly, the data used to create
your slides is the data model.
1:37
Maybe your presentation is about earnings,
or the results of experiments, or
1:41
maybe your favorite episodes of Dr. Who.
1:44
Whatever it is, you collected data and
1:46
formatted it in some way
to be viewed on the screen.
1:48
This pattern is the same for
Android apps and games.
1:51
The model is the data
that you use in the app,
1:53
the view is how it is actually
displayed on the screen, and
1:56
the presenter manipulates the data and
objects in some way.
1:59
So next,
how does this apply to our current app?
2:02
Think for a moment about how this
interactive story is set up.
2:05
On the phone, itself, is the screen
that the user interacts with.
2:08
The layouts we create for our activities
are what are shown on the screen, so
2:11
let's call those the views.
2:14
The layout is a presentational aspect
of the app that the user views and
2:16
interacts with.
2:20
Now what piece of our app presents
these layouts, these views?
2:21
What part of our code manages how and
when things are shown?
2:24
And what should happen
when things are tapped?
2:27
That's right, the activities.
2:30
These are the presenters in
the MVP pattern in Android.
2:31
As a side note,
2:35
you will sometimes hear the presenter
referred to as the controller.
2:35
Since it controls both the view
on the screen and the data model.
2:39
We'll stick with presenter, though,
as that's more widely used for Android.
2:42
If you're interested in reading more about
the exact differences between MVP and MVC,
2:45
check the teacher's notes for some links.
2:49
The presenter's also responsible for
interacting with the data model behind
2:51
the scenes, just like how you create and
display slides in a presentation.
2:55
Presenter is what adds, changes,
or deletes data in the model
2:59
based on what the user is doing
on the screen, or the view.
3:02
So that leaves us with the model.
3:06
All the data in our app,
3:07
as well as the logic around that data,
goes into model objects.
3:09
It helps to make the connection by
thinking of the phrase, data model.
3:12
Now what kind of data
are we about to work with?
3:16
That's right, in this case,
it's our story.
3:18
The data can really come from anywhere.
3:20
We'll build it right into our app here,
but it could be data from someplace
3:22
on the web or even elsewhere on the phone,
like from a gallery of photos.
3:25
Wherever we get our data from,
the model object's represented, and
3:28
our presenter gets it ready for display.
3:32
Presenter may even adapt the data,
like when we take a long time step and
3:34
convert it to a shortened representation
that may differ from region to region.
3:38
When we follow this MVP pattern,
3:42
the model does not communicate
with the view in any way.
3:44
This way the model object can
be separated from the view and
3:47
reused across other views
without having to repeat or
3:50
re-implement some of
the same functionality.
3:53
This setup is the basic architecture for
most Android apps.
3:56
The core Android code that we use to
develop apps has been designed in
3:59
such a way that we're kind of
forced to follow this pattern.
4:02
The model is whatever data
we need to work with.
4:05
The views are the actual
user interface of an app.
4:08
The layouts of buttons and
other things are the XML layout files.
4:10
And the presenter is the activity where
we do stuff behind the scenes and
4:14
update the view or the data model.
4:17
As you go further with Android
development, you may even come across
4:19
variations of these patterns
that have the same basic ideas,
4:22
but differ in some of the details for
very specific reasons.
4:24
We'll eventually talk more about
those in later courses and workshops.
4:28
But understanding the basic MVP
architecture is some key groundwork.
4:31
All right, so that tells us a bit about
what MVP is and how it's used in our apps.
4:35
But we're still missing the why behind it.
4:39
Don't worry, though,
that's coming up next in the next video.
4:42
You need to sign up for Treehouse in order to download course files.
Sign up