This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Consumer Functional Interface!
You have completed Consumer Functional Interface!
Preview
Consumers can save the day and help with the execute around method.
The code to this project can be found on GitHub.
Changes are made to the live
branch.
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
So ideally what would be beautiful
would be if we could get one of these
0:00
connections and this is the code that we
want them to use to express themselves.
0:04
We don't want them to worry
about how to get the connection.
0:09
So we can do a trick called
the execute around method.
0:12
Now this has been around for a while, but
0:17
functional interfaces really
help drive this home.
0:18
Okay so.
0:22
Let's take the dance and
put it someplace else.
0:23
Someplace that can be shared.
0:26
So let's do that.
0:28
So let's go ahead I'm
gonna copy this code.
0:28
And I'm going to go into the legacy
database file and let's just go ahead.
0:32
Let's make a new static method here,
so we'll make a public static.
0:36
It's not gonna return anything.
0:41
And let's say with connection.
0:43
And what we'll do is we will have
0:47
a consumer of type connection.
0:52
And we'll call that action.
0:56
And it is in that package.
0:59
So now basically what we want to do,
I'm just going to paste this here.
1:03
Well, we'll go ahead and import that,
1:07
even though I'm going to
get rid of it really quick.
1:08
I'm gonna take away what it is
that we want them to do here.
1:10
And I want to say action.except, right?
1:13
So that the functional
interface has an accept method.
1:15
And we've said that it can
only accept the connections,
1:19
so we're going to pass
on the connection here.
1:21
So that's it.
1:23
Now believe it or not, so what happens
is we're gonna pass on this action and
1:25
it's gonna be connection.
1:28
Now because it's a functional interface,
we can replace that with a lambda.
1:28
So let's go ahead,
let's use it, what you say?
1:32
So we'll come up here.
1:34
I'm gonna get rid of this stuff here.
1:37
Leave what we had there.
1:39
And we'll say LegacyDatabase and
then we have this .withConnection.
1:41
And it is looking for a lambda, so
we're gonna say a connection and
1:47
then I'm gonna open this up, I'm gonna
grab these guys here, I move this up.
1:52
All right, now we've got a bit of
a problem as you can see there's
1:57
an unhandled exception now.
2:01
This happens quite a bit
when you use lambdas.
2:02
So let's fix this with the way
that IntelliJ suggests for us.
2:05
So we'll surround it with a try and catch.
2:09
[SOUND] That's pretty gross,
but let's go ahead and
2:12
make sure that we are working as expected.
2:15
So I'm gonna run this again.
2:17
Look at that, it's still working.
2:20
It's still doing the new in the called,
but look, it's cleaner, but
2:22
it's kind of gross, right?
2:25
It's kind of gross that
we have this try in here.
2:26
So let's not add new dances, so
2:29
we can actually make our
own functional interface.
2:32
And let's do that right after this.
2:35
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