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 Intro to Java Web Development with Spark!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Let’s use the Data Access Objects pattern to build an interface that you can use to store your data. You will not be connecting to a database in this course, but this is one way to ensure that your code will work when you do.
Learn more:
- The Spark site has some great tutorials. Here is one on using sql2o with Spark which will get you up and running with a database.
- Data Access Object Pattern and a pretty good tutorial on them.
- Check out our Hibernate Basics Course!
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
[NOISE] So
let's keep moving on our project.
0:00
I find that working on the model is
a great way to shake out a prototype.
0:06
So let's think through our problem a bit.
0:11
We obviously need to have an idea,
which has a title.
0:13
And the idea'll also have a creator.
0:17
Now, we'll worry about how to
keep track of votes in a bit.
0:19
First, let's create the ability to
add ideas and then retrieve them.
0:22
Now, to me, that sounds like we
need to store things somewhere.
0:25
I mean, what we wanna do is make it so
that even if our web server restarts,
0:29
that our data is saved someplace,
or persistent.
0:34
That's a little out of scope for
this project, though, so
0:37
check the teacher's notes for more info.
0:40
So let's do this.
0:42
Let's use an interface and
0:43
we'll build a prototype implementation
that just uses normal data structures.
0:44
Sound good?
0:48
That way, later we can swap out
a database implementation or
0:49
anything for that matter.
0:53
As long as it meets our contract,
we'll be good to go.
0:53
That seems like a pretty light way to
do things in its micro framework land.
0:57
Now, larger frameworks provide some of
this capability out of the box, but our
1:01
micro framework here doesn't necessarily
force us into any direction at all.
1:05
So what do you say we drop that contract.
1:09
So let's make a new class.
1:13
And we're gonna put it in a new package.
1:17
So we're gonna say model.Courseidea.
1:19
And this is where we will store
the data structure basically.
1:28
So we said that this is gonna have
a string [BLANK AUDIO] called title,
1:32
and we also said it's gonna have
a private string called creator.
1:39
So let's go ahead and we'll generate,
remember that was Cmd+N,
1:45
we're gonna generate the constructor.
1:50
And then, we'll take both of those.
1:54
Title and creator and while we're at it,
let's do another generation.
1:57
Let's generate some getters.
2:01
And while we're here, let's go ahead and
let's generate the equals and hashCode.
2:10
I'll just use the default one.
2:16
Great.
2:20
Made you type all that stuff out?
2:22
Sorry about that.
2:25
Now, the simplest way to get where we're
headed is to have a single interface that
2:26
accesses all we need data storage wise.
2:31
So let's build it and we'll talk about it.
2:34
So a commonly recognized way of naming
the way that you plan to access the data
2:36
is with the acronym DAO,
or Data Access Object.
2:40
This is a design pattern that's used
to abstract a way how you retrieve and
2:46
store data.
2:49
Check the teacher's notes for
more on this.
2:50
So what we wanna do is in our new model,
here,
2:52
we wanna make a new Java Class and
we're gonna change it to be an Interface.
2:55
And what we're gonna make that
3:01
called is the CourseIdeaDAO,
3:06
Data Access Object.
3:11
So all we really need for
this is a way to add new ideas.
3:14
So we wanna make something called add and
it will take a CourseIdea,
3:21
called an idea, and then we also
need to have a way to find them.
3:28
So let's do that.
3:33
Let's make a list of CourseIdeas
3:34
and it's returned from a method
that's called findAll.
3:40
Now note, because this is the interface,
3:43
we aren't writing any
of the implementation.
3:45
We're gonna do that now, so
let's add our silly implementation.
3:47
So let's make a new class
in this model here.
3:53
Make a new Java Class and we're going
to call it SimpleCourseIdeaDAO.
3:57
Now, i'm calling it simple just to make
sure that we remember that this is
4:04
a simple way of doing things,
this is not how we should actually do it.
4:09
So let's do this.
4:12
And what we want it to do
is wanna implement that
4:14
Data Access Object interface.
4:18
So that's the CourseIdeaDAO.
4:21
Right away,
it's gonna start complaining, and
4:24
it's gonna say that we need to
implement the methods, so let's do it.
4:25
We'll implement both of these guys.
4:29
Then, we're gonna need to have a private
list of ideas that will maintain,
4:35
cuz we're gonna do this all
using normal data structures.
4:41
We're not gonna be using a database.
4:45
So we'll put here, we'll put private
4:48
List<CourseIdea> and
we'll call those ideas.
4:52
And on the constructor, [BLANK AUDIO] we
5:00
will automatically just
create a new ArrayList.
5:05
[INAUDIBLE] So
we'll say ideas = new ArrayList.
5:11
Awesome.
5:20
And it's using the diamond method.
5:22
So now we just kinda do
what we would think.
5:25
So if somebody calls add,
we wanna add our idea to our list.
5:28
And then, just in case somebody tries
to get cheeky and does a findAll and
5:36
appends the list, let's go ahead and
make sure that we make a copy of the list.
5:42
So we're gonna say new ArrayList.
5:47
And remember, collections can take another
collection and it makes a new one from it.
5:51
So this is not actually
a reference to the list.
5:56
It is a brand new list,
so you can't append this.
5:59
You can't do findAll and then do your
own appending to it, make sense?
6:03
And now for
the part that's pretty crazy/cool, ready?
6:08
So we're gonna flip back to the app Main.
6:13
And in here, remember we're
inside of a main method here.
6:17
So let's go ahead and
make our model appear.
6:21
So we're gonna say CourseIdeaDAO,
and we're gonna call that dao.
6:26
So remember,
this CourseIdeaDAO is our Interface.
6:34
And this is our implementation.
6:40
So we're gonna implement
the SimpleCourseIdeaDAO.
6:42
Now, later if we wanted to, we would
switch that out with the database one.
6:44
So any one of these route methods
can now close over this DAO.
6:48
Does that make sense?
6:54
Now, I want to remind you this
should only be used for prototyping.
6:56
Do not do this at home.
7:00
This SimpleCourseIdeaDAO will
not survive a server restart.
7:02
We'll talk about more of it here in a bit.
7:08
But remember it's called simple,
don't do it at home.
7:10
Please, please, please remember
that this is just a prototype.
7:14
Do not run this live.
7:18
That simple CourseIdea,
data access object, is goofy and
7:20
won't survive a server restart.
7:24
You really wanna persist or
save this data for later use.
7:26
And our implementation is
really cutting corners.
7:30
So after we use this a bit, you will for
7:32
sure want to implement a data
based implementation, or
7:34
even a file based implementation, just so
you don't need to keep re-adding things.
7:37
That said though, let's go ahead and
7:42
add the ability to add,
right after this break,
7:44
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