This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Kotlin for Java Developers!
You have completed Kotlin for Java Developers!
Preview
We've got the M, now it's time for the VP. In this video we'll quickly create our view, and then we'll move on to creating our presenter!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Having completely finished our model, all
that's left is the presenter and the view.
0:00
Let's start by finishing up the view.
0:05
The view is just going to be an interface
with one function named update,
0:07
which updates the view.
0:12
So let's create a new kotlin file,
named GameView.
0:13
And let's choose Interface, for the kind.
0:21
Then let's add our update function,
fun_update.
0:25
And pass on our game model,
model, of GameModel.
0:30
And this way,
any class that implements this interface
0:33
will be notified when
the model updates and
0:37
we'll be able to use this model
parameter update the UI accordingly.
0:40
And that's as far as we're going to
get with the GameView in this course.
0:44
But don't worry,
0:48
the next course will be all about
how we can implement this GameView.
0:49
All right,
now that we're done with the game view,
0:52
all that's left is the presenter.
0:54
The presenter serves as a middleman
between our model and our view.
0:56
When a user takes an action on our view,
1:01
we're going to pass that
action along to our presenter.
1:04
Our presenter will then
tell the model to update.
1:07
And once the model's been updated,
1:10
it's going to pass that model to our
views update method to update the screen.
1:12
So let's create a new class.
1:16
Name GamePesenter.
1:18
And inside this class, let's start,
but declaring a function for
1:26
when the user taps on the deck,
fun onDeckTap().
1:31
And inside this function the first thing
we need to do is update the model,
1:36
which we don't have access to.
1:40
But never fear, colon's got us covered.
1:43
First, we need to realize that we're
only modeling one game of solitaire.
1:47
And we're only ever going to
model one game at a time.
1:51
So really, we should be able to
declare our game model as a singleton.
1:55
If you're not familiar with singletons,
2:00
it's basically just a class that
you can only initialize one time.
2:02
In Java, you have to program
this functionality yourself.
2:06
But in kotlin, it's super easy.
2:09
We just replace the word
class with the word object.
2:12
So let's head over to
our game model class.
2:15
And change this from class to object.
2:19
And notice that it changes
the icon over here, as well.
2:25
Our game model is now a singleton.
2:28
So instead of needing
an instance of this class,
2:30
Centaur Presenter,
we can just access it directly.
2:33
Back in our GamePresenter class,
inside the on DeckTap function, let's pick
2:37
up where we left off by passing this
on deck tap action along to our model.
2:41
GameModel.onDeckTap().
2:47
When we're dealing with
a singleton like our GameModel,
2:50
all we have to do to access the instance
is type the name of the object.
2:53
Cool, right?
2:57
Now, I know you might be worried that this
looks exactly the same ,as if onDeckTap
2:58
was a function and
our GameModels companion object.
3:03
But in kotlin,
singletons cannot have companion objects.
3:07
So, that's no problem at all.
3:12
Now that we've updated the model,
we need to update the view as well.
3:14
Unfortunately, getting access to the view
isn't going to be quite as easy.
3:17
But, we'll save that for the next video.
3:21
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up